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
watsonx.ai Dojo #2 生成AIを使ったアプリ開発入門編
oniak3ibm
PRO
0
230
エラーレスポンス設計から考える、0→1開発におけるGraphQLへの向き合い方
bicstone
5
1.5k
GenU導入でCDKに初挑戦し、悪戦苦闘した話
hideg
0
170
REXML改善のその後
naitoh
0
190
Hermes: Better Performance with Bytecode Translation (React Universe 2024)
tmikov2023
0
100
Securify_エンジニア採用資料
3shake
0
110
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
4
90k
Why Prism?
kddnewton
4
1.7k
What you can do with Ruby on WebAssembly
kateinoigakukun
0
170
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
1k
GraphQLの魅力を引き出すAndroidクライアント実装
morux2
3
780
Swiftコードバトル必勝法
toshi0383
0
170
Featured
See All Featured
The Invisible Side of Design
smashingmag
296
50k
Infographics Made Easy
chrislema
239
18k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
27
7.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.1k
How to name files
jennybc
75
98k
The Mythical Team-Month
searls
218
43k
What's new in Ruby 2.0
geeforr
340
31k
It's Worth the Effort
3n
182
27k
GitHub's CSS Performance
jonrohan
1030
450k
Documentation Writing (for coders)
carmenintech
65
4.3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
43
2k
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
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!