Difference between Kernel#caller and Kernel#caller_locations

Hi,

I am bit confused with the methods
Kernel#caller(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller)
and
Kernel#caller_locations(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller_locations).
Are there any differences between these 2 methods?

Thanks

On Jul 27, 2013, at 13:43 , Love U Ruby [email protected] wrote:

I am bit confused with the methods
Kernel#caller(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller)
and

Kernel#caller_locations(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller_locations).

Are there any differences between these 2 methods?

You mean… the same answer to the same question you asked in May?

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/407506

I really wish you’d stop abusing this list and learn how to learn on
your own. I don’t follow or participate in this list half as much as I
used to and you’re mostly to blame. The people who insist on playing
your games are also to blame, but not as much as you. The signal:noise
on this list is degraded because of you.

Am 27.07.2013 23:13, schrieb Ryan D.:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/407506

To be fair: I don’t think this aspect has been answered in that thread,
and it really is not so easy to spot in irb output
(since Thread::Backtrace::Location#inspect produces the same output
as the strings returned when using Kernel#caller).

I really wish you’d stop abusing this list and learn how to learn on your own.
[snip]

I think it has improved a lot in the past months.

Best regards,
Marcus

Am 27.07.2013 22:43, schrieb Love U Ruby:

Hi,

I am bit confused with the methods
Kernel#caller(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller)
and

Kernel#caller_locations(http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-caller_locations).

Are there any differences between these 2 methods?

Yes. Read the first sentences in the docs again!

caller: “Returns the current execution stack—an array containing strings
in the form file:line or file:line: in `method’.”

caller-locations: “Returns the current execution stack—an array
containing backtrace location objects.”

And see:

2.0.0-p247 :002 > caller_locations.first.class
=> Thread::Backtrace::Location
2.0.0-p247 :003 > caller.first.class
=> String

Regards,
Marcus

Am 10.10.2013 12:13, schrieb Love U Ruby:

===============================================================
foo = Foo.new

I ran the code as below :

[email protected]:~/Ruby$ ruby so.rb
[“so.rb:6:in sam2'", "so.rb:10:in'”]
[email protected]:~/Ruby$

In both case I am getting the same output.I am trying to understand the
differences,but in my code I am not able to catch it. I am doing
probably something wrong… Could you point me out there ?

I already gave you an example (which you snipped…) that demonstrates
the difference, here again:

2.0.0-p247 :001 > caller_locations.first.class
=> Thread::Backtrace::Location
2.0.0-p247 :002 > caller.first.class
=> String

They return objects of different classes. But note that inspect', which is used byp’ (or in irb), returns the same string for both:

2.0.0p247 :003 > puts caller_locations.first.inspect
“[…]/lib/ruby/2.0.0/irb/workspace.rb:86:in eval'" => nil 2.0.0p247 :004 > puts caller.first.inspect "[...]/lib/ruby/2.0.0/irb/workspace.rb:86:ineval’”
=> nil

That’s what I meant in my other post when I said:

“[…] it really is not so easy to spot in irb output
(since Thread::Backtrace::Location#inspect produces the same output
as the strings returned when using Kernel#caller).”

Regards,
Marcus

unknown wrote in post #1116913:

Am 27.07.2013 22:43, schrieb Love U Ruby:

Hi,

caller: “Returns the current execution stack—an array containing strings
in the form file:line or file:line: in `method’.”

caller-locations: “Returns the current execution stack—an array
containing backtrace location objects.”

===============================================================
File : so.rb

class Foo
def sam1
caller_locations
end
def sam2
sam1
end
end
foo = Foo.new
p foo.sam2

I ran the code as below :

[email protected]:~/Ruby$ ruby so.rb
[“so.rb:6:in sam2'", "so.rb:10:in'”]
[email protected]:~/Ruby$

Now I change the method to caller and ran the code again as below :

class Foo
def sam1
caller
end
def sam2
sam1
end
end
foo = Foo.new
p foo.sam2

I ran the code as below :

[email protected]:~/Ruby$ ruby so.rb
[“so.rb:6:in sam2'", "so.rb:10:in'”]
[email protected]:~/Ruby$

In both case I am getting the same output.I am trying to understand the
differences,but in my code I am not able to catch it. I am doing
probably something wrong… Could you point me out there ?

unknown wrote in post #1124174:

That’s what I meant in my other post when I said:

“[…] it really is not so easy to spot in irb output
(since Thread::Backtrace::Location#inspect produces the same output
as the strings returned when using Kernel#caller).”

Regards,
Marcus

Hi Marcus,

Thanks for your time. Ok… so I can then use any of them when I need
it…I mean - Is there any difference in the area of application where I
should use #caller but not #caller_locations,and vice-versa ?

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs