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
71
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
200
Other Decks in Programming
See All in Programming
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
460
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
830
Goで作る、開発・CI環境
sin392
0
260
チームのテスト力を総合的に鍛えて品質、スピード、レジリエンスを共立させる/Testing approach that improves quality, speed, and resilience
goyoki
5
1.1k
新メンバーも今日から大活躍!SREが支えるスケールし続ける組織のオンボーディング
honmarkhunt
5
8.4k
GPUを計算資源として使おう!
primenumber
1
210
フロントエンドのパフォーマンスチューニング
koukimiura
5
1.9k
Git Sync を超える!OSS で実現する CDK Pull 型デプロイ / Deploying CDK with PipeCD in Pull-style
tkikuc
4
310
顧客の画像データをテラバイト単位で配信する 画像サーバを WebP にした際に起こった課題と その対応策 ~継続的な取り組みを添えて~
takutakahashi
4
1.1k
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
2
150
型で語るカタ
irof
0
630
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
460
Featured
See All Featured
Writing Fast Ruby
sferik
628
62k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
For a Future-Friendly Web
brad_frost
179
9.8k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Code Reviewing Like a Champion
maltzj
524
40k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
YesSQL, Process and Tooling at Scale
rocio
173
14k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
970
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!