Asked 7 years ago
7 Jan 2017
Views 1141
noob

noob posted

difference between include and extend in Ruby?

what is difference between include and extend in Ruby?
Phpworker

Phpworker
answered Nov 30 '-1 00:00

it seems in Ruby , include will include files and extend will extend the class for inheritance . isn't it ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

include and extend is look similar at first glance in ruby
but if you see more closely some minor difference.
include also include peace of code and extend do same also but
include , include it as module's methods as instance methods.
extend , include it as module's methods as class methods.


module main
   def me
     puts "Me module main"
   end
 end

 class sub1
   include main
 end

 class sub2
   extend main
 end

sub1.new.me is valid invoke
sub2.me is valid invoke
you can feel difference between include and extend in Ruby
Post Answer