Stack Frames, Functions and Variables

The Wiki link is

robert

“Robert Klemme” bob.news@gmx.net schrieb im Newsbeitrag news:…

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

The Wiki link is
http://www.rubygarden.org/ruby?MethodHooks

Well, your code has a problem with this, no ?

svg% cat b.rb
#!/usr/bin/ruby
require 'hooks.rb'

module A
   define_hook(:length) do
      puts "A#length"
      prior
   end
end

class String
   include A
   define_hook(:length) do
      puts "I'm calculating the length"
      prior
   end
end

str = "abcde"

x = str.length
puts x
svg%

svg% b.rb
./hooks.rb:65: warning: instance_methods parameter will default to 'true' in Jan 2004
./hooks.rb:58:in `class_eval': (eval):1:in `class_eval': undefined method `length' for module `A' (NameError)
        from ./hooks.rb:58:in `class_eval'
        from ./hooks.rb:58:in `define_hook'
        from ./b.rb:5
svg%

Guy Decoux

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200306111041.h5BAftC20368@moulon.inra.fr

The Wiki link is
http://www.rubygarden.org/ruby?MethodHooks

Well, your code has a problem with this, no ?

Yes, because you define the hook before the original method is defined.
If I find the time, I might try a second version that solves this problem.
After all this was just an explorative study and not production code.
Anyway, thank’s for pointing that out.

svg% cat b.rb
#!/usr/bin/ruby
require ‘hooks.rb’

module A
define_hook(:length) do
puts “A#length”
prior
end
end

Cheers

robert

If I find the time, I might try a second version that solves this problem.

Perhaps best to wait than matz solve this problem, no ?

Guy Decoux

svg% cat b.rb
#!./ruby
module A
   def length:before
      puts "A#length:before"
   end
end

class String
   include A
   def length:before
      puts "String#length:before"
   end
end

str = "abcde"

x = str.length
puts x
svg%

svg% b.rb
String#length:before
A#length:before
5
svg%

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200306111435.h5BEZmU01334@moulon.inra.fr

If I find the time, I might try a second version that solves this
problem.

Perhaps best to wait than matz solve this problem, no ?

Is there work under way for a general hooking mechanism?

robert

Is there work under way for a general hooking mechanism?

See [ruby-talk:73196]

Guy Decoux

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200306111435.h5BEZmU01334@moulon.inra.fr

If I find the time, I might try a second version that solves this
problem.

Perhaps best to wait than matz solve this problem, no ?

Is there work under way for a general hooking mechanism?

Matz is planning one, but the syntax
is not final. And he prefers an explicit
:before, :after, and so on.

I assume he is right, that this is better,
but I am not sure why.

Hal

···

----- Original Message -----
From: “Robert Klemme” bob.news@gmx.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, June 11, 2003 11:33 AM
Subject: Re: Stack Frames, Functions and Variables.

“Hal E. Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:0f7f01c33039$29de27c0$0300a8c0@austin.rr.com

Is there work under way for a general hooking mechanism?

Matz is planning one, but the syntax
is not final. And he prefers an explicit
:before, :after, and so on.

What else is there - :in_between? :-))

I assume he is right, that this is better,
but I am not sure why.

:slight_smile:

One thing that comes to mind is, that this way one might be able to
implement preconditions and postconditions IOW DBC - depending on what the
hooks are allowed to do.

Cheers

robert