Five operators
( , ). , send , .
SimpleModule.send("class_hello_world")
, . , Math, .
SimpleClass.send(:class_hello_world)
, , . class_hello_world .
SimpleClass.new.send(:hello_world)
# i am SimpleClass method
.
SimpleModule.send("hello_world")
hello_world.
SimpleModule.new.send(hello_world)
#=> NoMethodError: undefined method `new' for SimpleModule:Module
.
include vs prepend
,
SimpleClass.include SimpleModule
SimpleClass.new.hello_world
SimpleClass ' hello_world . SimpleClass.
SimpleClass.ancestors
Ruby hello_world SimpleClass - - SimpleModule.
, , Module # prepend SimpleModule#hello_world SimpleClass#hello_world.
SimpleClass.prepend SimpleModule
SimpleClass.new.hello_world
SimpleClass.ancestors
, . SimpleModule ( ) . UnboundMethod # bind SimpleClass, call send.
sc = SimpleClass.new
um = SimpleModule.instance_method(:hello_world)
bm = um.bind(sc)
bm.call
sc.send(:hello_world)