Instantly share code, notes, and snippets.
this is one of the weirdest ruby thing's I've ever seen. I love it.
I love it too, because I can understand how ruby evaluates it (I think.) The default argument is evaluated as ruby, but in the same scope as the method, so setting local variables means they're set for the method as well. And the parenthesis make it one expression, which ruby is fine with. And then it uses the return value of that expression for the default value of the name variable.
Wow, really interesting. Never thought about this use case, but kind of makes sense.
Holy crap, look at all the forks! You've created a monster!
@caius @avdi - what is the use case of this trick ?
The only use case I can think of is lulz. I'd never let this through a code review.
@avdi :) I was scratching if I would ever end up using this trick...
def x y=((return 'lol'); y)
def x(y=(raise 'i <3 params')); y; end
def foo(something = (def foo(*); 'bar'; end)) 'qux' end
puts foo(10) # qux
puts foo # qux
puts foo(10) # bar
puts foo # bar