I have a method which I want to act differently depending on the scenario, so I want to be able to pass a block to the method, and execute if the block is given.
However, I am confused by the scope of the variable in the block I am passing in.
For example:
def original_method (a, b, opt = {id: nil, id_map: {}})
element_id = (opt[:id_map])
yield if block_given?
end
And the new method which passes the block:
def new_method(a, b, opt)
original_method (a, b, opt) do
if(element_id.include? "some text")
puts "it has some text"
end
end
end
But I get the error:
undefined local variable or method `element_id'
at the yield line.
Can this be done?
(opt[:id_map]