Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby 2.0 Keyword Arguments
Search
iancanderson
April 04, 2013
Programming
2
68
Ruby 2.0 Keyword Arguments
iancanderson
April 04, 2013
Tweet
Share
More Decks by iancanderson
See All by iancanderson
A TDD primer with Ruby (or something)
iancanderson
3
190
Other Decks in Programming
See All in Programming
GraphQLの魅力を引き出すAndroidクライアント実装
morux2
3
790
Increased Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
400
エラーレスポンス設計から考える、0→1開発におけるGraphQLへの向き合い方
bicstone
5
1.5k
2024 컴포즈 정원사
jisungbin
0
150
REXML改善のその後
naitoh
0
190
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
Jakarta EE meets AI
ivargrimstad
1
540
watsonx.ai Dojo #2 生成AIを使ったアプリ開発入門編
oniak3ibm
PRO
0
240
Using Livebook to build and deploy internal tools @ ElixirConf 2024
hugobarauna
0
250
Method Swizzlingを行うライブラリにおけるマルチモジュール設計
yoshikma
0
120
Why Prism?
kddnewton
4
1.7k
Scala におけるコンパイラエラーとの付き合い方
chencmd
2
430
Featured
See All Featured
Producing Creativity
orderedlist
PRO
340
39k
How To Stay Up To Date on Web Technology
chriscoyier
786
250k
Designing with Data
zakiwarfel
98
5k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
38
9.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
322
23k
Ruby is Unlike a Banana
tanoku
96
11k
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Large-scale JavaScript Application Architecture
addyosmani
508
110k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
Transcript
Ruby 2.0 Keyword Arguments Ian Anderson
Ruby 1.9 - Hash parameters def foo(opts = {}) bar
= opts.fetch(:bar, 'default') puts bar end foo # 'default' foo(bar: 'baz') # 'baz'
Ruby 2.0 - Keyword arguments def foo(bar: 'default') puts bar
end foo # 'default' foo(bar: 'baz') # 'baz'
What if..? def foo(bar: 'default') puts bar end foo('baz') #
ArgumentError: wrong number of arguments (1 for 0)
PRINCIPLE OF MOST SURPRISE
Keyword arguments + (optional) options def foo(bar, baz: 'qux', **options)
puts [bar, baz, options] end foo(1, option: 2) # [1, 'qux', {:option => 2}] foo(1, option: 2, baz: 'notqux') # [1, 'notqux', {:option => 2}]
Janky required keyword arguments def foo(bar: raise(ArgumentError)) puts bar end
foo # ArgumentError: ArgumentError foo(bar: 'baz') # 'baz'
Block keyword arguments whoooaaaa define_method(:foo) do |bar: 'baz'| puts bar
end foo # 'baz' foo(bar: 'qux') # 'qux'
Concrete example - Ruby 1.9 def accepts_nested_attributes_for(*attr_names) options = {
:allow_destroy => false, :update_only => false } options.update(attr_names.extract_options!) options.assert_valid_keys( :allow_destroy, :reject_if, :limit, :update_only ) end
Concrete example - Ruby 2.0 def accepts_nested_attributes_for(*attr_names, allow_destroy: false, update_only:
false reject_if: nil, limit: nil ) end
Anatomy of a Ruby 2.0 method def foo( {required_arguments, ...}
{optional_arguments, ...} {*rest || more_required_arguments...} {keyword_arguments: "defaults"...} {**rest_of_keyword_arguments} {&block_capture} )
Method of the year. def foo(req1, req2, maybe1 = "42",
maybe2 = maybe1.upcase, *args, named1: 'foo', named2: bar(named1, req2), **options, &block) end def bar(a, b) # ... end
Try out Ruby 2.0 today!