Getting the current module(s), class name and method in Ruby 1.9


#1

Hi, first of all I’m sorry since I already did this question some time
ago
(but I ca’t find it now).

Basically I want the following:


module MyModule

class MyClass
def show_log
puts “I’m here: ###### FIXME ######”
end
end

end

my_class = MyModule::MyClass.new
my_class.show_log
=> I’m here: MyModule::MyClass#show_log

When I did this question I remember that it was not possible with Ruby
1.8 but
it was feasible in Ruby 1.9.

Could you please show me how to get it?
Thanks a lot.


#2

Iñaki Baz C. wrote:

  puts "I'm here: ###### FIXME ######"

When I did this question I remember that it was not possible with Ruby 1.8 but
it was feasible in Ruby 1.9.

Could you please show me how to get it?
Thanks a lot.

Something I prefer to use is the LINE and FILE constants. They
work a treat!

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.


#3

El Lunes 30 Marzo 2009, Michael M. escribió:

Something I prefer to use is the LINE and FILE constants. They
work a treat!

That’s ok, but it’s not what I’m looking for.
Basically I want a logger that shows the current module(s), class name
and
method name.

Thanks.


#4

El Martes 31 Marzo 2009, Sean O’Halpin
escribió:> > def show_log


Robert K. - see ruby-talk 205150 & 205950).
end

module MyModule
class MyClass
def show_log
puts “#{self.class}.#{this_method}”
end
end
end

MyModule::MyClass.new.show_log # => MyModule::MyClass.show_log

Fantastic! Thanks a lot.


#5

On Mon, Mar 30, 2009 at 10:32 PM, Iñaki Baz C. removed_email_address@domain.invalid wrote:

 puts "I'm here: ###### FIXME ######"

When I did this question I remember that it was not possible with Ruby 1.8 but
it was feasible in Ruby 1.9.

Could you please show me how to get it?
Thanks a lot.

Hi,

The following works in both 1.8.6 and 1.9.1 (calling_method is by
Robert K. - see ruby-talk 205150 & 205950).

module Kernel
private
def calling_method(level = 1)
caller[level] =~ /`([^’]*)’/ and $1
end

def this_method
calling_method
end
end

module MyModule
class MyClass
def show_log
puts “#{self.class}.#{this_method}”
end
end
end

MyModule::MyClass.new.show_log # => MyModule::MyClass.show_log

Regards,
Sean


#6

El Martes 31 Marzo 2009, Iñaki Baz C.
escribió:> “#{self.to_s}.#{caller[0][/`(.*)’/, 1]}”

module MM

MM::AA.class_method
=> “MM::AA.class_method”

MM::AA.new.instance_method
=> “MM::AA#instance_method”

Hi again. Wouldn’t make sense to have such features in Ruby core instead
of
having to parse “caller[0][/`(.*)’/, 1]” and so?

Is it possible to open a feature request for it? or is it too late for
such
wishes in 1.9?

Thanks.


#7

El Martes 31 Marzo 2009, Iñaki Baz C.
escribió:

Fantastic! Thanks a lot.

If somebody is interested, I’ve implemented the above code adding Class
methods logging feature:


module Kernel

def this_method
if self.class == Class
“#{self.to_s}.#{caller[0][/(.*)'/, 1]}" else "#{self.class}##{caller[0][/(.*)’/, 1]}”
end
end
private :this_method

end

module MM
class AA
def self.class_method
puts this_method
end

def instance_method
  puts this_method
end

end
end

MM::AA.class_method
=> “MM::AA.class_method”

MM::AA.new.instance_method
=> “MM::AA#instance_method”

Regards.