instance_eval

Discussion in 'Ruby' started by Abhishek Singh, Jul 29, 2009.

  1. 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Abhishek Singh, Jul 29, 2009
    #1
    1. Advertising

  2. Abhishek Singh

    Robert Dober Guest

    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]
     
    Robert Dober, Jul 29, 2009
    #2
    1. Advertising

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Abhishek Singh, Jul 29, 2009
    #3
  4. Hi --

    On Wed, 29 Jul 2009, Abhishek Singh wrote:

    > 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
     
    David A. Black, Jul 29, 2009
    #4
  5. 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. :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Abhishek Singh, Jul 30, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    98
    Mauricio Fernández
    Jul 17, 2003
  2. Tim Bates

    instance_eval and Class Variables

    Tim Bates, Aug 6, 2003, in forum: Ruby
    Replies:
    0
    Views:
    112
    Tim Bates
    Aug 6, 2003
  3. Aron Griffis

    instance_eval and constants

    Aron Griffis, Feb 24, 2004, in forum: Ruby
    Replies:
    2
    Views:
    90
    Aron Griffis
    Feb 24, 2004
  4. Joel VanderWerf

    instance_eval from C ext

    Joel VanderWerf, Apr 26, 2004, in forum: Ruby
    Replies:
    3
    Views:
    186
    Joel VanderWerf
    May 3, 2004
  5. lopex

    instance_eval

    lopex, Aug 17, 2004, in forum: Ruby
    Replies:
    10
    Views:
    200
    Richard Dale
    Aug 17, 2004
Loading...

Share This Page