Difference between class_eval and just adding the function?

Discussion in 'Ruby' started by Aryk Grosz, Feb 7, 2008.

  1. Aryk Grosz

    Aryk Grosz Guest

    Lets say I have

    module A
    class B
    def t
    puts "hi"
    end
    end
    end

    And I want to add a function to it. Whats the difference between:

    module A
    class B
    def new_function
    puts "foo"
    end
    end
    end

    ***AND***

    A::B.class_eval do
    def new_function
    puts "foo"
    end
    end

    Is there any difference?
    --
    Posted via http://www.ruby-forum.com/.
    Aryk Grosz, Feb 7, 2008
    #1
    1. Advertising

  2. Aryk Grosz

    Day Guest

    On Feb 7, 2008 5:55 PM, Aryk Grosz <> wrote:
    > And I want to add a function to it. Whats the difference between:
    >
    > module A
    > class B
    > def new_function
    > puts "foo"
    > end
    > end
    > end
    >
    > ***AND***
    >
    > A::B.class_eval do
    > def new_function
    > puts "foo"
    > end
    > end
    >
    > Is there any difference?


    Kind of hard to read all this nesting without some kind of spacing for
    help. Or did it just get stripped for me, for some reason?

    Anyway, I think, if you're writing out the method ahead of time,
    there's not a real difference. However, if you wanted to create
    methods on the fly based on something else (who knows what...), you'd
    want, of the two, class_eval. Though, really, you'd probably want to
    use add_method, or whatever the name of the function for doing that is
    (can't remember right now).


    Ben
    Day, Feb 8, 2008
    #2
    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. jakk
    Replies:
    4
    Views:
    12,025
  2. Youssef Mesri
    Replies:
    15
    Views:
    563
    Earl Purple
    Jan 19, 2006
  3. Minkoo Seo
    Replies:
    1
    Views:
    202
    Pit Capitain
    Feb 8, 2007
  4. Raj Singh
    Replies:
    4
    Views:
    134
    Ryan Ingram
    Jan 29, 2008
  5. Kyung won Cheon
    Replies:
    1
    Views:
    86
    Peña, Botp
    Aug 1, 2008
Loading...

Share This Page