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

scp command usage guide

ROR HEROKU Working with file storage like paperclip in heroku for free of cost ( paperclip and dropbox) paperclip-dropbox config | ROR, HEROKU, file storage, paperclip, in heroku, free of cost, paperclip, dropbox, paperclip-dropbox config