Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > instance_eval

Reply
Thread Tools

instance_eval

 
 
lopex
Guest
Posts: n/a
 
      08-17-2004
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
 
Reply With Quote
 
 
 
 
lopex
Guest
Posts: n/a
 
      08-17-2004
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

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      08-17-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      08-17-2004
>>>>> "l" == lopex <(E-Mail Removed)> writes:

l> x.instance_eval("a")

l> is a plain function call

What do you expect with ?

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


Guy Decoux


 
Reply With Quote
 
lopex
Guest
Posts: n/a
 
      08-17-2004
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
 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      08-17-2004
>>>>> "l" == lopex <(E-Mail Removed)> writes:

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


 
Reply With Quote
 
lopex
Guest
Posts: n/a
 
      08-17-2004
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
 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      08-17-2004
>>>>> "l" == lopex <(E-Mail Removed)> writes:

l> so why the given example outputs nil: ?

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


Guy Decoux


 
Reply With Quote
 
lopex
Guest
Posts: n/a
 
      08-17-2004
ts wrote:


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


Thanks. It's clear now for me.

Marcin Mielzynski
 
Reply With Quote
 
Mark Firestone
Guest
Posts: n/a
 
      08-17-2004
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" <(E-Mail Removed)>
To: "ruby-talk ML" <(E-Mail Removed)>
Cc: <(E-Mail Removed)>
Sent: Tuesday, August 17, 2004 4:04 PM
Subject: Re: instance_eval


> >>>>> "l" == lopex <(E-Mail Removed)> writes:

>
> l> so why the given example outputs nil: ?
>
> An instance variable belong to an object, not to a scope.
>
>
> Guy Decoux
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing in CGI parameters to an .rhtml file with StringIO#instance_eval Francis Hwang Ruby 2 10-07-2004 07:50 PM
instance_eval from C ext Joel VanderWerf Ruby 3 05-02-2004 11:00 PM
instance_eval and constants Aron Griffis Ruby 2 02-24-2004 11:15 PM
instance_eval and Class Variables Tim Bates Ruby 0 08-06-2003 06:05 AM
instance_eval("class") error/bug??? walter@mwsewall.com Ruby 1 07-17-2003 05:29 PM



Advertisments