class User
def initialize(name)
@name = name
end
def hello
puts
"Hello,#{@name}!"
end
def method_missing(_)
hello
end
end
user = User.new(
'Gregory')
method = user.method(:hello)
user.set_instance_variable(:@name, "Not Only Code")
method.call() # prints "Hello, Not Only Code!"
class Proc
def call(obj)
obj.send(@symbol_used_to_create_proc)
end
end
class User
def method_missing(_)
hello
end
end
user.i_am_a_lizard_king
# prints "Hello, Gregory!"
user.i_can_do_everything
# prints "Hello, Gregory!"
require 'method_source'
# external gem
method_source = user.method(:hello).source
method_body =method_source.split(
"\n")[1...-1].join(
";")
user.instance_eval(method_source)
def hello
puts
"Hello,#{@name}!"
end
更多精彩推荐
☞ 实用小技能 | 用 Word 和 Excel 自制一个题库自判断答题系统! ☞中国数据库产业的“高地战事” ☞ 融资 2000 万美元后,他竟将核心代码全开源,这……能行吗? ☞ Get了!用Python制作数据预测集成工具 | 附代码 ☞ 学会这10大高性能开发技术,轻松躲过裁员名单! ☞ 小心!你可能玩了假的DeFi ![]()
点分享 ![]()
点点赞 ![]()
点在看