Confusion with the UnboundMethod#source_location method

I read the UnboundMethod#source_location
(Class: UnboundMethod (Ruby 2.0.0))
says:

(a) Returns the Ruby source filename and line number containing this
method (b) nil if this method was not defined in Ruby (i.e. native)

class Foo
def snow
“wow”
end
end

Foo.instance_method(:snow) # => #<UnboundMethod: Foo#snow>
Foo.instance_method(:snow).source_location

=> [“-”, 2]

–in the above case - why does the “source file name” not come ?

String.instance_method(:to_str) # => #<UnboundMethod: String#to_str>
String.instance_method(:to_str).source_location

=> nil

–in the above case - why does the nil come ?

you run the file directly in ruby right? “-” means it was defined in
ruby interpreter itself and not in a file.

irb would show “(irb)”

String.instance_method(:to_str) # => #<UnboundMethod: String#to_str>
String.instance_method(:to_str).source_location

=> nil

–in the above case - why does the nil come ?

like you already said: “nil” means native, so to_str is defined in C for
the MRI interpreter

Hans M. wrote in post #1119477:

you run the file directly in ruby right? “-” means it was defined in
ruby interpreter itself and not in a file.

irb would show “(irb)”

Thank you very much “Mr. Expert”… I understood now.

Here is my try:-

kirti@kirti-Aspire-5733Z:~$ irb
2.0.0p0 :001 > class Foo
2.0.0p0 :002?> def wow
2.0.0p0 :003?> 1
2.0.0p0 :004?> end
2.0.0p0 :005?> end
=> nil
2.0.0p0 :006 > Foo.instance_method(:wow).source_location
=> ["(irb)", 2]
2.0.0p0 :007 > require “./SO.rb”
LoadError: cannot load such file – ./SO.rb
from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in
require' from /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:inrequire’
from (irb):7
from /home/kirti/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `’
2.0.0p0 :008 > require ‘fileutils’
=> true
2.0.0p0 :009 > FileUtils.pwd
=> “/home/kirti”
2.0.0p0 :010 > FileUtils.cd ‘/home/kirti/ruby’
=> nil
2.0.0p0 :011 > require “./SO.rb”
=> true
2.0.0p0 :012 > Foo.instance_method(:snow).source_location
=> ["/home/kirti/ruby/SO.rb", 2]
2.0.0p0 :013 >

Hey Hans,

Can you explain your comment that nil means native? Nil usually is
returned
when it can’t find the source location so nil is returned since ruby
always
returns something.

Not saying you’re wrong, just trying to understand how you came to that
conclusion as it differs from what I know of Ruby.

Thanks

Thanks for clarifying for me.

for an external binding or functions that are defined inside C or C++
code,
once compiled ruby cant get the location anymore (because the compiled
lib files are not parse-able)

its also the same for the parameters