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

Discussion in 'Ruby' started by Iñaki Baz Castillo, Mar 30, 2009.

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

    Basically I want the following:

    =2D--------------
    module MyModule

    class MyClass
    def show_log
    puts "I'm here: ###### FIXME ######"
    end
    end

    end


    my_class =3D MyModule::MyClass.new
    my_class.show_log
    =3D> I'm here: MyModule::MyClass#show_log
    =2D--------------

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

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



    =2D-=20
    I=C3=B1aki Baz Castillo <>
    Iñaki Baz Castillo, Mar 30, 2009
    #1
    1. Advertising

  2. Re: Getting the current module(s), class name and method in Ruby1.9

    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.
    =======================================================================
    Michael Malone, Mar 30, 2009
    #2
    1. Advertising

  3. El Lunes 30 Marzo 2009, Michael Malone escribi=C3=B3:
    > 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=
    =20
    method name.

    Thanks.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
    Iñaki Baz Castillo, Mar 30, 2009
    #3
  4. On Mon, Mar 30, 2009 at 10:32 PM, I=F1aki Baz Castillo <> wrot=
    e:
    > Hi, first of all I'm sorry since I already did this question some time ag=

    o
    > (but I ca't find it now).
    >
    > Basically I want the following:
    >
    > ---------------
    > module MyModule
    >
    > =A0class MyClass
    > =A0 =A0def show_log
    > =A0 =A0 =A0puts "I'm here: ###### FIXME ######"
    > =A0 =A0end
    > =A0end
    >
    > end
    >
    >
    > my_class =3D MyModule::MyClass.new
    > my_class.show_log
    > =3D> 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 =3D 1)
    caller[level] =3D~ /`([^']*)'/ 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 # =3D> MyModule::MyClass.show_log

    Regards,
    Sean
    Sean O'Halpin, Mar 30, 2009
    #4
  5. El Martes 31 Marzo 2009, Sean O'Halpin escribi=F3:
    > On Mon, Mar 30, 2009 at 10:32 PM, I=F1aki Baz Castillo <> wr=

    ote:
    > > 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 =3D MyModule::MyClass.new
    > > my_class.show_log
    > > =3D> 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 =3D 1)
    > caller[level] =3D~ /`([^']*)'/ 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 # =3D> MyModule::MyClass.show_log



    =46antastic! Thanks a lot.

    =2D-=20
    I=F1aki Baz Castillo <>
    Iñaki Baz Castillo, Mar 30, 2009
    #5
  6. El Martes 31 Marzo 2009, I=F1aki Baz Castillo escribi=F3:

    > Fantastic! Thanks a lot.


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

    =2D---------------
    module Kernel
    =09
    def this_method
    if self.class =3D=3D Class
    "#{self.to_s}.#{caller[0][/`(.*)'/, 1]}"
    else
    "#{self.class}##{caller[0][/`(.*)'/, 1]}"
    end
    end
    private :this_method
    =09
    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
    =3D> "MM::AA.class_method"

    MM::AA.new.instance_method
    =3D> "MM::AA#instance_method"
    =2D---------------


    Regards.


    =2D-=20
    I=F1aki Baz Castillo <>
    Iñaki Baz Castillo, Mar 31, 2009
    #6
  7. El Martes 31 Marzo 2009, I=F1aki Baz Castillo escribi=F3:
    > El Martes 31 Marzo 2009, I=F1aki Baz Castillo escribi=F3:
    > > 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 =3D=3D 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
    > =3D> "MM::AA.class_method"
    >
    > MM::AA.new.instance_method
    > =3D> "MM::AA#instance_method"
    > ----------------



    Hi again. Wouldn't make sense to have such features in Ruby core instead of=
    =20
    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=
    =20
    wishes in 1.9?

    Thanks.


    =2D-=20
    I=F1aki Baz Castillo <>
    Iñaki Baz Castillo, Mar 31, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,774
    ding feng
    Jun 25, 2003
  2. Mitko Haralanov
    Replies:
    17
    Views:
    417
    Ben Finney
    Oct 20, 2006
  3. Tobiah
    Replies:
    6
    Views:
    274
    kedra marbun
    Jul 6, 2010
  4. Matthew Heidemann
    Replies:
    14
    Views:
    229
    eT Ma
    Mar 22, 2011
  5. Ralph Shnelvar

    Getting name of current method

    Ralph Shnelvar, Sep 4, 2010, in forum: Ruby
    Replies:
    4
    Views:
    86
    Robert Klemme
    Sep 4, 2010
Loading...

Share This Page