Mike Austin's Blog

Wednesday, October 31, 2007

Default arguments

It surprised me that Ruby let me refer to another argument from a default argument definition. I'm not sure if that's a bad thing, but it's exactly what I wanted. In this case, the idiom is "use the argument, else use the original value:
def add_child( child, origin = child.origin )
child.parent = self
child.origin = origin
@children.push( child )
end
I could have simply used:
child.origin = origin if origin
but that seems less declarative (for lack of a better word). I don't like conditionals :)