Ruby 1.9 allows you to define variables that are only local to a block, and not to close variables with the same name in the outer scope:
x = 10
proc { |;x|
x = 20
}.call
x #=> 10
I would like this behavior to be the default for some blocks that I define - without using |; x, y, z | syntax (note the semicolon).
I don't think Ruby allows this initially, but can this functionality be hacked?
I have one solution at the moment, but it is pretty ugly as it requires checking which locales changed at the end of the block and then returning them to their values before the block. I do not mind if your decision requires you to specify which variables are block-local at the beginning of the ie blockscope(:x) { x = 20 }
source
share