# Question about instance\_eval

**URL:** <https://www.ruby-forum.com/t/question-about-instance-eval/94608>\
**Category:** Ruby\
**Created:** [May 8, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608 "2006-05-08T22:31:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 8, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608/1 "2006-05-08T22:31:41Z")

</div>

I’m trying to use instance\_eval in a project I’m doing, and I  
extracted a very simple example:

class IETest  
attr\_accessor :foo  
end

t = IETest.new  
t.instance\_eval { self.foo = ‘foo’ }  
puts t.foo

This works as expected. However if I do  
t.instance\_eval { foo = ‘foo’ }

then t.foo =\> nil

I also know that t.instance\_eval { @foo =\> ‘foo’ } would work in this  
case. However, I’m trying to set a Rails attribute, so I have to go  
through the method accessor. Is there any way that I can make this  
work with just { foo = ‘foo’ } instead of { self.foo = ‘foo’ } ?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 8, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608/2 "2006-05-08T22:40:50Z")

</div>

On May 8, 2006, at 3:30 PM, Pat M. wrote:

> t.instance\_eval { foo = ‘foo’ }

Ruby will always see the above as a local variable declaration.  
There is no way around that.

> I also know that t.instance\_eval { @foo =\> ‘foo’ } would work in this  
> case. However, I’m trying to set a Rails attribute, so I have to go  
> through the method accessor.

You’ve showed one way to handle it. Another is the use  
write\_attribute(), if we are talking about ActiveRecord.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 8, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608/3 "2006-05-08T22:40:58Z")

</div>

No. Define method missing to dispatch calls to “foo” to the accessor,  
and  
change your block just a little bit. E.g.

class IETest  
def method\_misssing(sym, \*args, &blk)  
# dispatches any methods missing to “assignment” version of method.  
self. **send** (sym + “=”, \*args, &blk)  
end  
end

t = IETest.new  
t.instance\_eval { foo ‘foo’ } # notice lack of assignment operator  
puts t.foo

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 8, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608/4 "2006-05-08T23:08:49Z")

</div>

On 5/8/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You’ve showed one way to handle it. Another is the use  
> write\_attribute(), if we are talking about ActiveRecord.
> 
> James Edward G. II

Can I call pass in an object to instance\_eval? Let me show you the  
full class…

class ScenarioGenerator  
def initialize  
@rules = []  
end

def add\_rule(&r)  
@rules \<\< r  
end

def generate  
s = Scenario.new  
num\_players = 0  
@rules.each do |r|  
r.call(s)  
end

```
s.create_players(num_players) if num_players > 0
s

```

end  
end

The reason I went to r.call(s) is so that I can do  
g = ScenarioGenerator.new  
g.add\_rule { |s| s.sb = 200 }

s then becomes the Scenario that I created.

However I want to be able to modify num\_players with a rule, so I need  
instance\_eval. I still need to be able to pass in the Scenario object  
though. How could I do that?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 8, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/question-about-instance-eval/94608/5 "2006-05-08T23:27:14Z")

</div>

On 5/8/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > through the method accessor.
> 
> def generate
> 
> Pat

Apparently I can’t do that, so I changed the design up a bit so that I  
could do {|s| s.num\_opponents = 3}.

Thanks for the help James and Justin, I really appreciate it.

Pat
