# Instance\_eval

**URL:** <https://www.ruby-forum.com/t/instance-eval/172878>\
**Category:** Ruby\
**Created:** [July 29, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/instance-eval/172878 "2009-07-29T12:10:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@abhishek](https://www.ruby-forum.com/u/abhishek)\
**Post date:** [July 29, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/instance-eval/172878/1 "2009-07-29T12:10:19Z")

</div>

hii  
i’m using instance\_eval inside method\_missing to define a new method at  
run time.  
bt at the time of calling if i call newly defined method once it doesn’t  
give any value…so for getting the value from newly defined method i  
have to call it twice…  
is there any way to get value from method by calling only once.

---

<div class="post-metadata">

**Author:** ![abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@abhishek](https://www.ruby-forum.com/u/abhishek)\
**Post date:** [July 29, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/instance-eval/172878/2 "2009-07-29T12:17:05Z")

</div>

It would be easier to help you if you posted your code.  
Cheers  
Robert

> 

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@abhishek](https://www.ruby-forum.com/u/abhishek)\
**Post date:** [July 29, 2009, 12:24pm UTC](https://www.ruby-forum.com/t/instance-eval/172878/3 "2009-07-29T12:24:41Z")

</div>

def method\_missing(method\_name,\*args)  
for @@y in(0…@@e-1) do  
if ("#{method\_name}"==@@a[0][@@y])  
@@index=@@y  
break  
else if (@@y==@@e-1)  
puts “This method (#{method\_name})does not belong to any field  
value … !!”  
end  
end  
end  
instance\_eval " def #{method\_name}; puts ‘#{@@a[@@x][@@index]}’; end"  
end

and when i want to access this newly defined method i’m doing  
following:-

ro1=RecordOne.find(“subject”,“algorithm”)  
ro1.author  
ro1.author

i’m not able to get value at single method call…☹

---

<div class="post-metadata">

**Author:** ![abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@abhishek](https://www.ruby-forum.com/u/abhishek)\
**Post date:** [July 29, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/instance-eval/172878/4 "2009-07-29T12:47:32Z")

</div>

Hi –

On Wed, 29 Jul 2009, Abhishek S. wrote:

> end  
> i’m not able to get value at single method call…☹  
> A typical pattern is to redefine the method and then call it using  
> send:

class C  
def method\_missing(m,\*args,&block)  
instance\_eval “def #{m}; puts ‘In new method!’; end”  
send(m)  
end  
end

C.new.blah # In new method!

In your case that might look something like this (untested):

def method\_missing(method\_name,\*args)  
index = @@a[0].index(method\_name)  
if index  
instance\_eval \<\<-EOM  
def #{method\_name}  
puts #{@@a[@@x][index]}  
end  
EOM  
send(method\_name)  
else  
super  
end  
end

I don’t know what all the class variables are for, and I’ve tried to  
eliminate the ones that seem to be serving only as local variables.

David

---

<div class="post-metadata">

**Author:** ![abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@abhishek](https://www.ruby-forum.com/u/abhishek)\
**Post date:** [July 30, 2009, 7:04am UTC](https://www.ruby-forum.com/t/instance-eval/172878/5 "2009-07-30T07:04:56Z")

</div>

David A. Black wrote:

> class C  
> def method\_missing(m,\*args,&block)  
> instance\_eval “def #{m}; puts ‘In new method!’; end”  
> send(m)  
> end  
> end
> 
> C.new.blah # In new method!

thanx…i got that. 🙂
