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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
iancanderson
April 04, 2013
Programming
2
77
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
210
Other Decks in Programming
See All in Programming
AI活用のコスパを最大化する方法
ochtum
0
110
ふん…おもしれぇ Parser。RubyKaigi 行ってやるぜ
aki_pin0
0
110
atmaCup #23でAIコーディングを活用した話
ml_bear
4
690
Oxlint JS plugins
kazupon
1
1.1k
並行開発のためのコードレビュー
miyukiw
2
2k
文字コードの話
qnighy
41
15k
Geminiの機能を調べ尽くしてみた
naruyoshimi
0
170
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
2k
株式会社 Sun terras カンパニーデック
sunterras
0
1.9k
AI巻き込み型コードレビューのススメ
nealle
2
2.3k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
240
Claude Codeと2つの巻き戻し戦略 / Two Rewind Strategies with Claude Code
fruitriin
0
190
Featured
See All Featured
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
190
A Soul's Torment
seathinner
5
2.3k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
190
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
180
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
140
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
110
The Invisible Side of Design
smashingmag
302
51k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
750
Context Engineering - Making Every Token Count
addyosmani
9
680
Designing for Timeless Needs
cassininazir
0
140
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.3k
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!