Created
February 7, 2011 18:05
-
-
Save bagwanpankaj/814848 to your computer and use it in GitHub Desktop.
Ruby 1.9 Default Arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ')' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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