Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base.extend vs. base.class_eval extend

Tags:

ruby

Is this syntax functionally equivalent

  def self.included(base)
    base.class_eval do
      extend ClassMethods
    end
  end

to this?

  def self.included(base)
    base.extend ClassMethods
  end
like image 972
Bryan Locke Avatar asked Nov 10 '09 20:11

Bryan Locke


1 Answers

The only relevant difference is that only classes respond to "class_eval", whereas both classes and instances respond to "extend".

If you don't plan on using your method with object instances, then they are equivalent, though the second implementation can be used to add instance methods to a particular instance, while the first one cannot.

like image 128
Marcos Toledo Avatar answered Sep 22 '22 08:09

Marcos Toledo