The method itself shouldn't care; it returns the same result regardless whether an argument was passed explicitly or not.
What am I missing? :(
The method itself shouldn't care; it returns the same result regardless whether an argument was passed explicitly or not.
What am I missing? :(
[1, 2, 3].inject(:+)
[1, 2, 3].inject(0, :+)
[1, 2, 3].inject(nil, :+) # doesn't work
Rubinius uses a magic ``undefined`` value for this: def inject(initial=undefined, sym=undefined, &block)So I wanted to know in that method if nil was passed in by argument (as the parsed body was nil), or if no argument was passed, at which point the argument was set to nil as the default value by ruby. Having ruby set a `default = true` if no argument was passed was pretty much the only way to get myself out of that hole right then.
Of course, having been coded into that corner once, I'd never do it that way again, so I'm not sure I'll ever be in a corner I need to use it to get out of again. Still useful to have the technique in the back pocket though, just in case!
It's problematic though, because writing a thin wrapper becomes a pain in the ass.
* One to do the lookup and potentially explode; this always takes an argument.
* A second to provide the default.
Have the caller call the appropriate one. User provides params[:search] (or some other "explicitly trying to search"), then call the first. If not, call the latter.