Jiri Kremser Jiri Kremser - 2 years ago 90
Ruby Question

Get the fully qualified name of method

Is there a way to obtain the string representation of fully qualified path to the method?

Consider following IRB session:

> foo = File.stat("/")
> magic(foo.ctime) or foo.ctime.magic
=> "File::Stat#ctime"

Is there any built-in function that does the "magic"? If not, can I get the string dynamically by doing some reflection?

note: I am aware of this question, but I can't use the pry.

Answer Source

One might call Object#method, returning an instance of Method:

#⇒ File::Stat#ctime()

The fully qualified method name might be extracted e.g. from a Method#to_s:

foo.method(:ctime).to_s[/(?<=\A#<Method: ).*(?=>)/]
#⇒ "File::Stat#ctime"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download