Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby 2.0 Keyword Arguments
Search
iancanderson
April 04, 2013
Programming
93
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby 2.0 Keyword Arguments
iancanderson
April 04, 2013
More Decks by iancanderson
See All by iancanderson
A TDD primer with Ruby (or something)
iancanderson
3
220
Other Decks in Programming
See All in Programming
Crash Auto Pipelines
veronikapj
1
400
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
4
3.7k
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
1.9k
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
240
ActionController meets Protocol Buffers
sorah
1
800
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
960
Modernizing iOS Apps
seyfoyun
0
150
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
250
AI Augmentation Techniques with Liberty
ivargrimstad
1
420
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
2.3k
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
740
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
370
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
480
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
520
Prompt Engineering for Job Search
mfonobong
0
580
Technical Leadership for Architectural Decision Making
baasie
3
640
It's Worth the Effort
3n
188
29k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
910
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Optimising Largest Contentful Paint
csswizardry
37
4k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Un-Boring Meetings
codingconduct
0
490
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!