[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Iñaki Baz Castillo

3/30/2009 9:32:00 PM

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 <ibc@aliax.net>

6 Answers

Michael Malone

3/30/2009 9:37:00 PM

0

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


Iñaki Baz Castillo

3/30/2009 9:56:00 PM

0

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 <ibc@aliax.net>

Sean O'Halpin

3/30/2009 10:07:00 PM

0

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

Iñaki Baz Castillo

3/30/2009 10:27:00 PM

0

El Martes 31 Marzo 2009, Sean O'Halpin escribi=F3:
> On Mon, Mar 30, 2009 at 10:32 PM, I=F1aki Baz Castillo <ibc@aliax.net> 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 <ibc@aliax.net>

Iñaki Baz Castillo

3/30/2009 11:18:00 PM

0

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 <ibc@aliax.net>

Iñaki Baz Castillo

3/31/2009 7:55:00 PM

0

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 <ibc@aliax.net>