As you know, ruby always tries hard to get out of the programmer’s way most of the time. One good example is when u invoke a method that doesn’t require any argument:
1 2 3 4 5 6 |
|
Almost always, to invoke mm
, mm
is preferred over mm()
, doing
it the mm()
way is so un-rubyish. Yet this flexibility tripped us
over today, consider the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
When we ran the above spec, we get
undefined method 'macho' for "alien":String (NoMethodError)
.. why ??
Here’s our intended behaviour:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
This is what happened instead:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
To fix the problem, we can either:
avoid confusing names by renaming the local variable
thing
to_thing
& amend all its intended usage accordingly (which includelet(:thing) { send(thing) }
tolet(:thing) { send(_thing) }
), ORbe explicte when invoking method by rewriting
let(:subject) { thing }
aslet(:subject) { thing() }