instance_eval

A

Abhishek Singh

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.
 
R

Robert Dober

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


--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

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

[Antoine de Saint-Exup=E9ry]
 
A

Abhishek Singh

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

Attachments:
http://www.ruby-forum.com/attachment/3910/NewRecord.rb
 
D

David A. Black

Hi --

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

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 / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
 
A

Abhishek Singh

David said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top