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

Hi, for logging purposes I’d like to print the Modules, Class and Method
in
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
Class name by doing “self.class.to_s”.

Thanks a lot.

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

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”:
=> “App::Transport::Server::TCP”

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

Thanks.

Iñaki Baz C. wrote:

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.

El Domingo, 6 de Abril de 2008, Tim H. escribió:

Iñaki Baz C. wrote:

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.

Thanks, I’ll try it.

On Apr 6, 9:25 am, Iñaki Baz C. [email protected] wrote:

El Domingo, 6 de Abril de 2008, Tim H. escribió:

Iñaki Baz C. wrote:

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.

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  #=> :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.

On 06.04.2008 14:39, Iñaki Baz C. wrote:

end
=> “App::Transport::Server::TCP”

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

Yes, from caller().

Kind regards

robert

El Domingo, 6 de Abril de 2008, Trans
escribió:> #method.

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

Thanks :wink:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs