Jiri Kremser Jiri Kremser - 4 months ago 6x
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.


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"