Keyword Arguments aka Named Parameters in Ruby and
Keyword Arguments aka Named Parameters in Ruby (and Python) Matthew Cummings @matthewcummings 516 April 2015
About Me ● Currently Test Automation Engineer at Spiceworks, a heavy Ruby/Rails shop ● “Full Stack” test engineer. . . I do and have done a little bit of everything. ● Acronym soup - C, C++, Perl, Javascript, Java, bash, Python and now Ruby ● I love Python. . . ● And Ruby!
Python and Ruby ● My “dream” programming language would be a hybrid of the two, all the good parts, no bad parts. ● WITHOUT A GIL! Python also has a GIL. ● I’m still in the phase of comparing the two a lot. . . “Python, Ruby does THIS much better. ” ● “Now Ruby, do you know how easy THIS is in Python? ”
Won’t go into the gory details, but Both languages support several different ways of passing parameters. ● Ruby’s options/param hash <-> Python’s foobar(**kwargs) parameter ● Ruby’s splat foobar(*args) <-> Python’s foobar(*args), Python has no formal name for this. ● Regular old positional args are the same: Ruby foobar(a, b, c) <-> Python foobar(a, b, c)
Parameters vs. Arguments This is a bit pedantic, but there is a difference. def foobar(x, y) -> x and y are parameters foobar(1, 2) -> 1 and 2 are arguments
However. . . Everyone uses the terms parameter and argument interchangeably. : )
Python’s Keyword Arguments Python has had keyword arguments forever: def foobar(param 1=”default value”): print param 1 > foobar() default value > foobar("non-default value") non-default value
Why they’re cool ● Provide a default value. ● Argument order does not matter def foobar(param 1="default value", param 2="hooray"): print param 1 + param 2 > foobar(param 1="default value", param 2=", hooray!") default value, hooray! > foobar(param 2=", hooray!", param 1="default value") default value, hooray!
Ruby’s default arguments Pre-2. 0, Ruby only had default arguments, which look exactly like Python’s keyword parameters: def foobar(param 1=”default value”) print param 1 end > foobar default value
But. . . Unlike Python’s keyword arguments, parameter order DOES matter with Ruby’s default arguments. def foobar(x=1, y=2) puts "x is #{x}, y is #{y}" end > foobar(y=4, x=3) x is 4, y is 3 Not what we expected!
Enter Ruby 2. 0, Keyword Arguments Argument order doesn’t matter. def foobar(x: 1, y: 2) puts "x is #{x}, y is #{y}" end > foobar(y: 4, x: 3) x is 3, y is 4 That’s more like it!
Required Keyword arguments Python cannot do this: def foobar(x: , y: ) puts "x is #{x}, y is #{y}" end > foobar Argument. Error: missing keywords: x, y
So when/why use them? More than anything, I use them because they provide more runtime context when I call a method with incorrect arguments.
Compare this: def rectangle_area(width: , length: ) puts width * length end > rectangle_area(1) Argument. Error: missing keywords: width, length
With this: def rectangle_area(width, length) puts width * length end > rectangle_area(1) Argument. Error: wrong number of arguments (1 for 2)
That’s all for now ● Questions? ● Links: https: //robots. thoughtbot. com/ruby-2 -keywordarguments http: //stackoverflow. com/questions/20633412/m ixing-keyword-with-regular-arguments-in-ruby
- Slides: 16