Difference between include and extend in Ruby on Rails

  • include : mixes in specified module methods as instance methods in the target class
  • extend : mixes in specified module methods as class methods in the target class

So is the major difference just this or is a bigger dragon lurking? e.g.

  module ReusableModule
 
def module_method
    puts
"Module Method: Hi there!"
 
end
end

class ClassThatIncludes
  include
ReusableModule
end
class ClassThatExtends
  extend
ReusableModule
end

puts
"Include"
ClassThatIncludes.new.module_method       # "Module Method: Hi there!"
puts
"Extend"
ClassThatExtends.module_method              # "Module Method: Hi there!"

Comments

Popular posts from this blog

Rails Memcache issues

Enabling password authentication for new ec2 box | ssh, ssh config, EC2 setup, new user in EC2, PasswordAuthentication

What's the difference between "include" and "require" in Ruby?