Skip to content

Instantly share code, notes, and snippets.

@bagwanpankaj
Created February 7, 2011 18:05
Show Gist options
  • Save bagwanpankaj/814848 to your computer and use it in GitHub Desktop.
Save bagwanpankaj/814848 to your computer and use it in GitHub Desktop.
Ruby 1.9 Default Arguments
#rvm use 1.9.2
#ruby19 allows default arguments to be in beginning of method's arguments.
def test_default_argument(a = 1, b)
"a:#{a},b:#{b}"
end
test_default_argument(3)
#=> "a:1,b:3"
#ruby19 does not support default args to be used at both end of arguments.
def test_default_argument(a = 1, b, c = 3)
"a:#{a},b:#{b},c:#{c}"
end
#=> syntax error, unexpected '=', expecting ')'
#ruby19 continues to support default arguments at end of method's arguments.
def test_default_argument(a, b = 2)
"a:#{a},b:#{b}"
end
test_default_argument(3)
#=> "a:3,b:2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment