Download presentation
Presentation is loading. Please wait.
PublishΕλένη Δασκαλόπουλος Modified over 5 years ago
1
Keyword Arguments aka Named Parameters in Ruby (and Python)
Matthew Cummings @matthewcummings516 April 2015
2
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!
3
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?”
4
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)
5
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
6
However. . . Everyone uses the terms parameter and argument interchangeably. :)
7
Python’s Keyword Arguments
Python has had keyword arguments forever: def foobar(param1=”default value”): print param1 > foobar() default value > foobar("non-default value") non-default value
8
Why they’re cool Provide a default value.
Argument order does not matter def foobar(param1="default value",param2="hooray"): print param1 + param2 > foobar(param1="default value", param2=", hooray!") default value, hooray! > foobar(param2=", hooray!", param1="default value")
9
Ruby’s default arguments
Pre-2.0, Ruby only had default arguments, which look exactly like Python’s keyword parameters: def foobar(param1=”default value”) print param1 end > foobar default value
10
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!
11
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!
12
Required Keyword arguments
Python cannot do this: def foobar(x:, y:) puts "x is #{x}, y is #{y}" end > foobar ArgumentError: missing keywords: x, y
13
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.
14
Compare this: def rectangle_area(width:, length:) puts width * length
end > rectangle_area(1) ArgumentError: missing keywords: width, length
15
With this: def rectangle_area(width, length) puts width * length end
ArgumentError: wrong number of arguments (1 for 2)
16
That’s all for now Questions? Links:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.