Is posible to get the Module(s), Class and Method names in which weare?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, for logging purposes I'd like to print the Modules, Class and Method in=
=20
with the logger is called. Imagine this case:


module App
module TransportLayer
class Server
def get_data()
...
...
logger.info "We are in XXXXXXX"
end
end
end
end


I'd like to see:
"We are in App::TransportLayer::Server#get_data"

Is it possible in anyway? The only I know how to do if getting the current=
=20
Class name by doing "self.class.to_s".

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Domingo, 6 de Abril de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:
module App
module TransportLayer
class Server
def get_data()
...
...
logger.info "We are in XXXXXXX"
end
end
end
end


I'd like to see:
"We are in App::TransportLayer::Server#get_data"

Is it possible in anyway? The only I know how to do if getting the current
Class name by doing "self.class.to_s".

Ok sorry, I can get the modules and classes via "self.class.to_s":
=3D> "App::Transport::Server::TCP"

The only I miss if the method name, is ti possible?

Thanks.


=2D-=20
I=C3=B1aki Baz Castillo
 
T

Tim Hunter

Iñaki Baz Castillo said:
The only I miss if the method name, is ti possible?

In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to
parse the output from Kernel#caller.
 
I

Iñaki Baz Castillo

El Domingo, 6 de Abril de 2008, Tim Hunter escribi=C3=B3:
In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to
parse the output from Kernel#caller.

Thanks, I'll try it.

=2D-=20
I=C3=B1aki Baz Castillo
 
T

Trans

El Domingo, 6 de Abril de 2008, Tim Hunter escribi=F3:



Thanks, I'll try it.

Look at Facets: facets/ruby.rb. There is 1.8 implementation of
#__method__.

# Retreive the current running method name.
#
# def tester; __method__; end
# tester #=3D> :tester
#
# Technically __callee__ should provided alias names,
# where __method__ should not. But we'll have to
# leave that distinction to Ruby 1.9+.

def __method__
/\`([^\']+)\'/.match(caller(1).first)[1].to_sym
end

T.
 
R

Robert Klemme

El Domingo, 6 de Abril de 2008, Iñaki Baz Castillo escribió:


Ok sorry, I can get the modules and classes via "self.class.to_s":
=> "App::Transport::Server::TCP"

The only I miss if the method name, is ti possible?

Yes, from caller().

Kind regards

robert
 
I

Iñaki Baz Castillo

El Domingo, 6 de Abril de 2008, Trans escribi=F3:
El Domingo, 6 de Abril de 2008, Tim Hunter escribi=F3:

Thanks, I'll try it.

Look at Facets: facets/ruby.rb. There is 1.8 implementation of
#__method__.

# Retreive the current running method name.
#
# def tester; __method__; end
# tester #=3D> :tester
#
# Technically __callee__ should provided alias names,
# where __method__ should not. But we'll have to
# leave that distinction to Ruby 1.9+.

def __method__
/\`([^\']+)\'/.match(caller(1).first)[1].to_sym
end

Thanks ;)

=2D-=20
I=F1aki Baz Castillo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top