Instance_eval

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.

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]

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…:frowning:

Hi –

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

end
i’m not able to get value at single method call…:frowning:
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

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. :slight_smile: