Saturday, February 23, 2008

Ruby Code Snippets #3

Here's a simple Ruby code puzzle for you: if an argument is optional in Ruby, what do you think the result of the method would be if you passed 'nil' to arg2?
def mymethod(arg1, arg2='default')
Well, (as expected?) if you pass a nil as an argument, then there is something being passed, and hence 'default' won't be passed:
mymethod('foo', nil) #=> nil
And if you passed it just a single argument, here's what it'll look like:
mymethod('foo') #=> 'default'


Post a Comment