我想知道我是否可以强迫这种情况发生
class A
def bomb ; "bomb" ; end
end
class B ; end
bomb = A.instance_method(:bomb)
b = B.new
bomb.bind(b)
Run Code Online (Sandbox Code Playgroud)
目前它抛出错误TypeError:bind参数必须是A的实例
我发现这对于我可以用这些未绑定的方法做的非常有限,可能性有点限制.在这些情况下(我不仅仅指幂等函数)它会有意义吗?并且执行错误就足够了,如果我将处理A中未在B中复制的变量,我真的很想知道如何强制这个绑定.
例如,我有以下自定义类和模块:
module SimpleModule
def hello_world
puts 'i am a SimpleModule method'
end
def self.class_hello_world
puts 'i am a SimpleModule class method'
end
end
class SimpleClass
def hello_world
puts 'i am SimpleClass method'
end
def self.class_hello_world
puts 'i am a SimpleClass class method'
end
end
Run Code Online (Sandbox Code Playgroud)
我尝试使用方法在类和模块中调用这些方法 send
SimpleClass.send(class_hello_world) # work
SimpleClass.new.send(hello_world) # work
SimpleModule.send(class_hello_world) # work
SimpleModule.new.send(hello_world) # not work
SimpleModule.send(hello_world) # not work
Run Code Online (Sandbox Code Playgroud)
换句话说,我不知道如何调用hello_world从SimpleModule.如果该方法之前使用self定义,则可能.
我需要这样做,因为我想实现一个"自定义包含":包括从模块到另一个类的所有方法.
请告诉我怎么做.