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

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.

--
Iñaki Baz Castillo <ibc@aliax.net>

Iñaki Baz Castillo wrote:

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.

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.

Hi,

The following works in both 1.8.6 and 1.9.1 (calling_method is by
Robert Klemme - 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

···

On Mon, Mar 30, 2009 at 10:32 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

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.

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.

···

El Lunes 30 Marzo 2009, Michael Malone escribió:

Something I prefer to use is the __LINE__ and __FILE__ constants. They
work a treat!

--
Iñaki Baz Castillo <ibc@aliax.net>

Fantastic! Thanks a lot.

···

El Martes 31 Marzo 2009, Sean O'Halpin escribió:

On Mon, Mar 30, 2009 at 10:32 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> 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.

Hi,

The following works in both 1.8.6 and 1.9.1 (calling_method is by
Robert Klemme - 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

--
Iñaki Baz Castillo <ibc@aliax.net>

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

···

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

Fantastic! Thanks a lot.

----------------
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.

--
Iñaki Baz Castillo <ibc@aliax.net>

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.

···

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

El Martes 31 Marzo 2009, Iñaki Baz Castillo 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"
----------------

--
Iñaki Baz Castillo <ibc@aliax.net>