阅读背景:

Ruby - 可以将块作为param作为实际块传递给另一个函数吗?

来源:互联网 

This is what I'm trying to do:

这就是我想要做的:

def call_block(in_class = "String", &block)
    instance = eval("#{in_class}.new")
    puts "instance class: #{instance.class}"
    instance.instance_eval{ block.call }
end


# --- TEST EXAMPLE ---
# This outputs "class: String" every time
"sdlkfj".instance_eval {  puts "class: #{self.class}" }

# This will only output "class: Object" every time
# I'm trying to get this to output "class: String" though
call_block("String") { puts "class: #{self.class}" }
de



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: