Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Nuby with instance_eval question

Reply
Thread Tools

Nuby with instance_eval question

 
 
Geoff Barnes
Guest
Posts: n/a
 
      08-27-2006
Wondering why this works the way it does:

1 class Klass
2
3 attr_accessor :attr1
4
5 def initialize (&block)
6 chum = "chum_in_Klass"
7 instance_eval(&block) # Why does instance_eval pick up
self.attr1 from Klass but not 'chum'??
8 eval_attr1
9 end
10 def eval_attr1
11 @ok = self.attr1.call
12 end
13 end
14
15 chum = "chum_in_main"
16
17 foo=Klass.new {
18 self.attr1 = proc { chum }
19 }
20
21 puts foo.inspect # -> foo.ok = "chum_in_main"
22
23 chum="chum_in_main_again"
24 foo.eval_attr1
25
26 puts foo.inspect # -> foo.ok = "chum_in_main_again"

I have a constructor that uses a block to intialize the object's
members. Nested on the RHS of a member assignment is a proc block. The
proc block carries the value of chum from the "main" namespace, whereas
I expected it would take the chum from the Klass. I figured the proc
would not be evaluated and bound to a namespace until the passed block
was instance_eval'd in the constructpr, and thereby pick up a binding
from Klass... This makes my head hurt!

The observed behaviour is what I want but I didn't think it was
possible. I want to make sure I'm not taking advantage of a "bug" or a
language ambgiuity. If someone could make my eval/proc light bulb come
on it would be much appreciated..

Thanks

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Matthew Johnson
Guest
Posts: n/a
 
      08-27-2006

> Wondering why this works the way it does:
>
> 1 class Klass
> 2
> 3 attr_accessor :attr1
> 4
> 5 def initialize (&block)
> 6 chum = "chum_in_Klass"
> 7 instance_eval(&block) # Why does instance_eval
> pick up
> self.attr1 from Klass but not 'chum'??
> 8 eval_attr1
> 9 end
> 10 def eval_attr1
> 11 @ok = self.attr1.call
> 12 end
> 13 end
> 14
> 15 chum = "chum_in_main"
> 16
> 17 foo=Klass.new {
> 18 self.attr1 = proc { chum }
> 19 }
> 20
> 21 puts foo.inspect # -> foo.ok = "chum_in_main"
> 22
> 23 chum="chum_in_main_again"
> 24 foo.eval_attr1
> 25
> 26 puts foo.inspect # -> foo.ok = "chum_in_main_again"
>
> I have a constructor that uses a block to intialize the object's
> members. Nested on the RHS of a member assignment is a proc
> block. The
> proc block carries the value of chum from the "main" namespace,
> whereas
> I expected it would take the chum from the Klass. I figured the proc
> would not be evaluated and bound to a namespace until the passed block
> was instance_eval'd in the constructpr, and thereby pick up a binding
> from Klass... This makes my head hurt!
>
> The observed behaviour is what I want but I didn't think it was
> possible. I want to make sure I'm not taking advantage of a "bug"
> or a
> language ambgiuity. If someone could make my eval/proc light bulb
> come
> on it would be much appreciated..


chum is a local variable in both places. instance_eval only affects
self and class variables, not local variables.

Matthew


 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      08-27-2006
Geoff Barnes wrote:
> Wondering why this works the way it does:
>
> 1 class Klass
> 2
> 3 attr_accessor :attr1
> 4
> 5 def initialize (&block)
> 6 chum = "chum_in_Klass"
> 7 instance_eval(&block) # Why does instance_eval pick up
> self.attr1 from Klass but not 'chum'??
> 8 eval_attr1
> 9 end
> 10 def eval_attr1
> 11 @ok = self.attr1.call
> 12 end
> 13 end
> 14
> 15 chum = "chum_in_main"
> 16
> 17 foo=Klass.new {
> 18 self.attr1 = proc { chum }
> 19 }


You are creating a closure here and, since 'chum' exists in
this scope (you would get an error otherwise), it is bound
here.

> 20
> 21 puts foo.inspect # -> foo.ok = "chum_in_main"
> 22
> 23 chum="chum_in_main_again"
> 24 foo.eval_attr1
> 25
> 26 puts foo.inspect # -> foo.ok = "chum_in_main_again"
>
> <snip />
>
> Thanks



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Geoff Barnes
Guest
Posts: n/a
 
      08-27-2006
OK, thanks for the responses. Let's see if I got this right :

Blocks passed to instance_eval are still bound to where ever they were
declared but instance_eval will use instance & class variables where
ever required.




--
Posted via http://www.ruby-forum.com/.

 
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
question about instance_eval, dynamic methods and blocks Chris Hall Ruby 1 09-14-2007 05:03 PM
nuby question: question marks in method names Edwin Eyan Moragas Ruby 0 08-30-2004 07:11 AM
amrita-nuby question: generate "id=" Harry Ruby 2 01-26-2004 06:54 PM
nuby question: f.rename(x,y) does not work Boris \BXS\ Schulz Ruby 4 01-03-2004 04:19 PM
nuby question re a method Van Jacques Ruby 0 12-10-2003 02:47 AM



Advertisments