Stack Frames, Functions and Variables

I have a number of questions:

  1. Is it possible to examine current stack frame in ruby. Or move up in
    the stack frame to find the local variables of the function that called
    you?

  2. Can I put a hook that gets called whenever anyone or anything accesses
    a function AND/OR a variable within an object? I would like to intercept
    that call to create a nice wrapper.

  3. This should be a really easy question to answer:

    Class Foo
    @test = "Hello World"
    end

    Class Bar
    @test = Foo.new

    def bar
    puts @test.@test (???)
    end
    end

How do I access a local variable of an object pointed to as local
variable. Is is a scoping problem? Are they just not visible? Should I
make them public? Should I stand on a crossroads at midnight swinging a
black cat in a circle thirteen times?

Please help.

Greg

“Many a man may look respectable, and yet be able to hide at will behind a
spiral staircase.”
- Pelham (Plum) Grenville Wodehouse

Hi,

  1. Is it possible to examine current stack frame in ruby. Or move up in
    the stack frame to find the local variables of the function that called
    you?

The current scope information can be retrieved by “binding” function.
The binding object can be used as optional 2nd argument to “eval” to
evaluate code in the binding’s context.

  1. Can I put a hook that gets called whenever anyone or anything accesses
    a function AND/OR a variable within an object? I would like to intercept
    that call to create a nice wrapper.

It’s still under the discussion. But AspectR can help you now.

  1. This should be a really easy question to answer:

Class Foo
@test = “Hello World”
end

Class Bar
@test = Foo.new

def bar
puts @test.@test (???)
end
end

How do I access a local variable of an object pointed to as local
variable. Is is a scoping problem? Are they just not visible? Should I
make them public? Should I stand on a crossroads at midnight swinging a
black cat in a circle thirteen times?

If you want to peek instance variables without making them public, use
instance_eval, e.g.

foo.instance_eval{@test}

gives you the value of foo’s instance variable @test.

						matz.
···

In message “Stack Frames, Functions and Variables.” on 03/06/11, Grzegorz Dostatni grzegorz@ee.ualberta.ca writes:

I hacked something together that provides for hooking of methods. Look
for a thread called “How I’d like method-wrapping to work…”. I can send
it to you directly if you want to.

I’ll put it on the wiki as soon as I can access it again.

robert

“Grzegorz Dostatni” grzegorz@ee.ualberta.ca schrieb im Newsbeitrag
news:Pine.LNX.4.44.0306101627070.3875-100000@e5-05.ee.ualberta.ca…

I have a number of questions:

  1. Is it possible to examine current stack frame in ruby. Or move up in
    the stack frame to find the local variables of the function that called
    you?

  2. Can I put a hook that gets called whenever anyone or anything
    accesses
    a function AND/OR a variable within an object? I would like to intercept
    that call to create a nice wrapper.

  3. This should be a really easy question to answer:

    Class Foo
    @test = “Hello World”
    end

    Class Bar
    @test = Foo.new

def bar
puts @test.@test (???)
end
end

How do I access a local variable of an object pointed to as local
variable. Is is a scoping problem? Are they just not visible? Should I
make them public? Should I stand on a crossroads at midnight swinging a
black cat in a circle thirteen times?

Please help.

Greg

"Many a man may look respectable, and yet be able to hide at will behind
a

···

spiral staircase."

  • Pelham (Plum) Grenville Wodehouse