A common pattern I find myself using in Ruby is the following:
foo = foos.find {|foo| foo.attribute == some_other_attribute }
In IDEs that check for shadowing, that line complains that foo
is being shadowed.
A tempting alternative is the following:
foo = foos.find {|f| f.attribute == some_other_attribute }
But that approach breaks the search feature in many IDEs - searching for f
within a complicated block is going to be a nightmare and searching for foo.attributes
within the project will miss this line of code.
The ugly solution I've been leaning toward is just prefixing the variable name with its scope:
foo = foo.find {|block_foo| block_foo.attribute == some_other_attribute }
While that's fine for short blocks, anything longer starts to generate a lot of noise from all the block_
's polluting the code.
Is there a standard Ruby way of doing this sort of assignment? Specifically, I'm looking for a solution that meets the following criteria:
- Variable names aren't abbreviated
- Variable names remain descriptive of what type of object they contain
- IDEs don't squawk about shadowing