class_eval

Discussion in 'Ruby' started by Derek Chesterfield, Sep 26, 2005.

  1. Stealing someone else's recent example code, what is the difference
    between this:

    > class A
    > def meth2
    > puts "I'm meth2. I try to access something I ain't
    > allowed: #{static_var}"
    > end
    > static_var = 3
    > define_method:)meth3) do
    > puts "I'm meth3. I'm allowed to access #{static_var}"
    > end
    > end
    > a = A.new
    > a.meth3
    > a.meth2


    and this, which seems to do the same thing, but without the class_eval:

    > class B
    > def meth2
    > puts "I'm meth2. I try to access something I ain't
    > allowed: #{static_var}"
    > end
    > class_eval do
    > static_var = 3
    > define_method:)meth3) do
    > puts "I'm meth3. I'm allowed to access #{static_var}"
    > end
    > end
    > end
    > b = B.new
    > b.meth3
    > b.meth2


    In other words, what does class_eval do that I'm not understanding?!
     
    Derek Chesterfield, Sep 26, 2005
    #1
    1. Advertising

  2. Derek Chesterfield wrote:
    > Stealing someone else's recent example code, what is the difference
    > between this:
    >
    >> class A
    >> def meth2
    >> puts "I'm meth2. I try to access something I ain't
    >> allowed: #{static_var}"
    >> end
    >> static_var = 3
    >> define_method:)meth3) do
    >> puts "I'm meth3. I'm allowed to access #{static_var}"
    >> end
    >> end
    >> a = A.new
    >> a.meth3
    >> a.meth2

    >
    > and this, which seems to do the same thing, but without the
    > class_eval:
    >
    >> class B
    >> def meth2
    >> puts "I'm meth2. I try to access something I ain't
    >> allowed: #{static_var}"
    >> end
    >> class_eval do
    >> static_var = 3
    >> define_method:)meth3) do
    >> puts "I'm meth3. I'm allowed to access #{static_var}"
    >> end
    >> end
    >> end
    >> b = B.new
    >> b.meth3
    >> b.meth2

    >
    > In other words, what does class_eval do that I'm not understanding?!


    In this case it's not really interesting to use class_eval. It's more
    interesting for cases like these

    >> class Foo;end

    => nil
    >> Foo.class_eval do

    ?> def bar() end
    >> end

    => nil
    >> Foo.new.bar

    => nil
    >> Foo.class_eval "def bar() \"bar\" end"

    => nil
    >> Foo.new.bar

    => "bar"
    >> x=10

    => 10
    >> Foo.class_eval do

    ?> define_method:)baz) { puts x }
    >> end

    => #<Proc:0x1016a660@(irb):14>
    >> Foo.new.baz

    10
    => nil

    And note the difference to instance_eval:

    >> Foo.instance_eval do

    ?> def ban() "ban" end
    >> end

    => nil
    >> Foo.new.ban

    NoMethodError: undefined method `ban' for #<Foo:0x100c3d58>
    from (irb):20
    >> Foo.ban

    => "ban">> Foo.instance_eval "def borg() 'org' end"
    => nil
    >> Foo.new.borg

    NoMethodError: undefined method `borg' for #<Foo:0x101b4e50>
    from (irb):23
    from :0
    >> Foo.borg

    => "org"

    HTH

    Kind regards

    robert
     
    Robert Klemme, Sep 26, 2005
    #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. Bertram Scharpf

    instance/class_eval

    Bertram Scharpf, Apr 28, 2005, in forum: Ruby
    Replies:
    1
    Views:
    81
    Trans
    Apr 29, 2005
  2. Replies:
    3
    Views:
    164
    David A. Black
    Nov 10, 2005
  3. Bertram Scharpf

    class_eval and braces

    Bertram Scharpf, Jan 27, 2006, in forum: Ruby
    Replies:
    1
    Views:
    109
    Eero Saynatkari
    Jan 27, 2006
  4. Replies:
    2
    Views:
    129
    James Edward Gray II
    Mar 8, 2006
  5. Replies:
    1
    Views:
    96
Loading...

Share This Page