Forum: Ruby help with call_stack

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Benjamin Paul Kay (Guest)
on 2007-04-15 08:20
(Received via mailing list)
Hi! I'm new to the mailing list, and I was hoping to get some help with
call_stack. I'm running Ruby 1.8.5, and I've installed the
call_stack-0.1.0.0 gem. Here's a quick test file, test.rb:

###
require 'call_stack'
call_stack_on

class TestClass
  def a
    call_stack(-1).each { |element| puts "class=#{element[0].to_s},
self=#{eval('self',element[4])},
self.class=#{eval('self',element[4]).class}" }
  end
  def b
    puts "Method a is called from self=#{self},
self.class=#{self.class}"
    a
  end
end

test = TestClass.new
puts "Method a is called from self=#{self}, self.class=#{self.class}"
test.a
test.b
###

Running it produces the following output:
$ ruby test.rb
Method a is called from self=main, self.class=Object
class=unknown, self=#<TestClass:0x2b30b0177800>, self.class=TestClass
class=TestClass, self=Kernel, self.class=Module
Method a is called from self=#<TestClass:0x2b30b0177800>,
self.class=TestClass
class=unknown, self=#<TestClass:0x2b30b0177800>, self.class=TestClass
class=TestClass, self=Kernel, self.class=Module
class=TestClass, self=Kernel, self.class=Module

Also tried adding "require 'breakpoint185'" to the beginning of test.rb.
That just made things worse:
$ ruby test.rb
Method a is called from self=main, self.class=Object
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=TestClass, self=Kernel, self.class=Module
Method a is called from self=#<TestClass:0x2abc3dc77f80>,
self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=unknown, self=#<TestClass:0x2abc3dc77f80>, self.class=TestClass
class=TestClass, self=Kernel, self.class=Module
class=TestClass, self=Kernel, self.class=Module

I'm a bit confused. In both cases I was expecting something more like:
Method a is called from self=main, self.class=Object
class=TestClass, self=#<TestClass:0x2b30b0177800>, self.class=TestClass
class=Module, self=Kernel, self.class=Module
Method a is called from self=#<TestClass:0x2b30b0177800>,
self.class=TestClass
class=TestClass, self=#<TestClass:0x2b30b0177800>, self.class=TestClass
class=TestClass, self=#<TestClass:0x2b30b0177800>, self.class=TestClass
class=Module, self=Kernel, self.class=Module

In particular, why is the the call_trace(-1)[n][0] not the same as
eval('self',call_trace(-1)[n][4]).class, and why does call_trace(-1)[1]
seem to point to the kernel when method a is called from inside method
b?

Thanks for your patience and help,
Benjamin Kay
This topic is locked and can not be replied to.