Search code examples
rubyclass-eval

Whats the difference between class_eval and class << className?


I am a Ruby starter. I found both of these are quite similar (in output), but i couldn't understand the difference in the below context. For example, I have a class

class Say
  def self.hello
    puts "hello"
  end
end

and can be extended like this

class << Say
  def hi
    puts "hi"
  end
end

and also like this

Say.class_eval do
  def self.bye
    puts "bye"
  end
end

When should I use << and when class_eval?


Solution

  • class_eval doesn't really have anything to do with class << className.

    A.class_eval do
       ...
    end
    

    is equivalent to

    class A
      ...
    end
    

    with a few differences. class_eval uses a block (or a string, but ignoring that for the moment) which means it closes over the containing lexical scope. In other words you can use local variables from the surrounding scope. The common class block introduces a brand new scope. Likewise you can create the block and pass it to many different class_eval's, and the body of the block will be executed in the context of the class you are calling class_eval on.

    class << className opens the singleton class of className, allowing you to define class methods.

    class << A
      def foo
        ...
      end
    end
    

    Is the same as

    def A.foo
      ...
    end
    

    Note that they are oly class methods if A happens to be a class (almost) all objects in ruby have singleton classes and you can define methods for them using either of those two syntaxes. The advantage of class << obj is mainly if you're defining many singleton methods in one go.