method
extend
v1_8_7_72 -
Show latest stable
- Class:
Object
extend(...)public
Adds to obj the instance methods from each module given as a parameter.
module Mod def hello "Hello from Mod.\n" end end class Klass def hello "Hello from Klass.\n" end end k = Klass.new k.hello #=> "Hello from Klass.\n" k.extend(Mod) #=> #<Klass:0x401b3bc8> k.hello #=> "Hello from Mod.\n"
1Note
extend adds class methods too
Because classes are objects. So for example:
module Ispeak
def says
"greetings aliens!"
end
end
module Ieat
def eats
"spinach"
end
end
module Inhabitant
def says
"I'm strong to the finish"
end
end
class Human
extend Ispeak # add class methods from Ispeak
include Inhabitant # add instance methods from Inhabitant
end
Human.extend Ieat # add class methods from Ieat
puts Human.says # -> greetings aliens!
puts Human.eats # -> spinach
popeye = Human.new
puts popeye.says # -> I'm strong to the finish