Ruby - Possible to pass a block as a param as an actual block to another function?

Posted by Markus O'Reilly on Stack Overflow See other posts from Stack Overflow or by Markus O'Reilly
Published on 2010-03-18T08:50:36Z Indexed on 2010/03/18 8:51 UTC
Read the original article Hit count: 353

Filed under:
|
|

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}" }

On the line where it says "instance.instance_eval{ block.call }", I'm trying to find another way to make the new instance variable run instance eval on the block. The only way I can think of to get it to do that is to pass instance_eval the original block, not as a variable or anything, but as a real block like in the test example.

Any tips?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about instance-eval