Instance_eval

Hi,

I'm wondering why the following code works:

class X
end

x=X.new
a="some variable"

p x.instance_eval("a")
x.instance_eval{p a}

why the x object knows the a variable ?

TIA

Marcin Mielzynski

lopex wrote:

Ok. the following works since a gets captured by clousure

x.instance_eval{p a}

but

x.instance_eval("a")

is a plain function call

···

Marcin Mielzynski

Hi --

···

On Tue, 17 Aug 2004, lopex wrote:

Hi,

I'm wondering why the following code works:

class X
end

x=X.new
a="some variable"

p x.instance_eval("a")
x.instance_eval{p a}

why the x object knows the a variable ?

instance_eval sets 'self' to the receiver (x in this case), but it's
still happening in the existing scope, where 'a' is defined.

David

--
David A. Black
dblack@wobblini.net

lopex wrote:

Hi,

I'm wondering why the following code works:

class X
end

x=X.new
a="some variable"

p x.instance_eval("a")
x.instance_eval{p a}

why the x object knows the a variable ?

Maybe this makes it clearer:

class X
end

x=X.new
a="some variable"

def m1(thing, &block)
block.call(thing)
end

def m2(thing)
thing.instance_eval{p a}
end

p x.instance_eval("a")
x.instance_eval{p a}
m1(x) {p a}
m2(x)

The m1() method works because it is passed a block where 'a' is in scope.
Then in m1 the block is called with that complete context, ie including
'a'.

Whereas the m2() method fails because instance_eval() runs within the
context of just 'thing', ie 'x', without its environment.

"some variable"
"some variable"
"some variable"
try_instance_eval.rb:12:in `m2': undefined local variable or method `a' for
#<X:0x3004c0d8> (NameError)

-- Richard

x.instance_eval("a")

is a plain function call

What do you expect with ?

   a = "some variable"
   p eval("a")

Guy Decoux

ts wrote:

What do you expect with ?

   a = "some variable"
   p eval("a")

I read in rdoc that instance_eval changes eval context to the receiver, so a thought the scope changes as well.

Marcin Mielzynski

I read in rdoc that instance_eval changes eval context to the receiver,
so a thought the scope changes as well.

It change self and the default class

svg% cat b.rb
#!/usr/bin/ruby
class A
end

a = A.new
b = A.new

a.instance_eval("def a() puts 'a' end")
a.a
b.a
svg%

svg% b.rb
a
./b.rb:10: undefined method `a' for #<A:0x4009a024> (NoMethodError)
svg%

Guy Decoux

ts wrote:

It change self and the default class

So it seems it uses the class accessor of self
(btw. the def construct works in this way ? haven't seen it is a method like e.g. private, protected etc.)

so why the given example outputs nil: ?

···

---
class A
     def initialize
# @v="value"
     end
end

a=A.new

@v="second value"

p a.instance_eval("@v")
---

I know that there is no instance object @v in 'a', but if the scope
remains the same, should we have an access to @v defined in outer object of class Object ?
Or is the the reason we always can use instance variable identifier even it was not defined sa far... ?

TIA

Marcin Mielzynski

so why the given example outputs nil: ?

An instance variable belong to an object, not to a scope.

Guy Decoux

ts wrote:

An instance variable belong to an object, not to a scope.

Thanks. It's clear now for me.

Marcin Mielzynski

I have some weird problems...

...am trying to run pico via pty. It works, except that if you run it
though pty, it wants to justify all your text.

tried nano, but it's control codes don't work right, but the justification
problems don't happen. Very odd.

Anyone know of a simple terminal mode editor for linux that will work via
pty? thanks...

Mark

"But Schindler is bueno! Senior Burns is El Diablo!"

···

--------------------------------------------------------------
Website - http://www.retrobbs.org
Tradewars - telnet tradewars.retrobbs.org
BBS - http://bbs.retrobbs.org:8000
IRC - irc.retrobbs.org #main
WIKI - http://www.tpoh.org/cgi-bin/tpoh-wiki

----- Original Message -----
From: "ts" <decoux@moulon.inra.fr>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Cc: <ruby-talk@ruby-lang.org>
Sent: Tuesday, August 17, 2004 4:04 PM
Subject: Re: instance_eval

> so why the given example outputs nil: ?

An instance variable belong to an object, not to a scope.

Guy Decoux