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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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 <[email protected]>
 
M

Michael Malone

Iñaki Baz Castillo said:
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

Iñaki Baz Castillo

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 <[email protected]>
 
S

Sean O'Halpin

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

Iñaki Baz Castillo

El Martes 31 Marzo 2009, Sean O'Halpin escribi=F3:
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 <[email protected]>
 
I

Iñaki Baz Castillo

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 <[email protected]>
 
I

Iñaki Baz Castillo

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 <[email protected]>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top