Getting name of current method

R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

Ruby has __FILE__ and __LINE__ to give the file and line omber where we are currently at in the code.

Consider
Class X
def my_method
m = ??? # I want m to be assigned "my_method"
end
end

How can I get m set properly?

I guess I could use define_method to define my_method and then pass the name into the function .. but that is so ugly and also flattens scope that I may not want flattened.
 
R

Ryan Davis

Ruby has __FILE__ and __LINE__ to give the file and line omber where =
we are currently at in the code.
=20
Consider
Class X
def my_method
m =3D ??? # I want m to be assigned "my_method"
end
end
=20
How can I get m set properly?
=20
I guess I could use define_method to define my_method and then pass =
the name into the function .. but that is so ugly and also flattens =
scope that I may not want flattened.

`ri Kernel.caller`
 
R

Run Paint Run Run

Ruby has __FILE__ and __LINE__ to give the file and line omber where we a= re currently at in the code.

Consider
=C2=A0Class X
=C2=A0 =C2=A0def my_method
=C2=A0 =C2=A0 =C2=A0m =3D ??? =C2=A0# I want m to be assigned "my_method"
=C2=A0 =C2=A0end
=C2=A0end

How can I get m set properly?
=3D> :my_method
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

Ryan,

Friday, September 3, 2010, 6:50:53 PM, you wrote:



RD> `ri Kernel.caller`

Thanks for pointing that out. Interesting and useful.


I have come up with two alternate methods and I'd like to ask the help of all those here to help me modify the first method so that it does what I want

- - - -
class X
@m = "method_1"
@s = <<-XXX
def #{@m}
puts "In #{@m}"
end
XXX

puts "#{__FILE__} @ #{__LINE__}"
puts @s
eval @s

m = "method_2"
eval("def #{m}; puts \"In #{m}\"; end")

end

x = X.new

x.method_1
x.method_2

- - -- -


The code, above, works fine in 1.8.6

Note that I have to define @m before @s. How can I set things up so that I can define @m after @s so that I can modify @m and get a new function?
 
R

Robert Klemme

Note that I have to define @m before @s. How can I set things up so
that I can define @m after @s so that I can modify @m and get a new
function?

What are you trying to achieve?

Kind regards

robert
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top