instance_eval

L

lopex

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
 
D

David A. Black

Hi --

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
 
T

ts

l> x.instance_eval("a")

l> is a plain function call

What do you expect with ?

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


Guy Decoux
 
L

lopex

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
 
T

ts

l> I read in rdoc that instance_eval changes eval context to the receiver,
l> 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
 
L

lopex

ts said:
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
 
T

ts

l> so why the given example outputs nil: ?

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


Guy Decoux
 
M

Mark Firestone

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" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, August 17, 2004 4:04 PM
Subject: Re: instance_eval
 
R

Richard Dale

lopex said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top