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
73
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
はじめてのMaterial3 Expressive
ym223
2
900
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
340
機能追加とリーダー業務の類似性
rinchoku
2
1.3k
rage against annotate_predecessor
junk0612
0
170
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
550
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
3.3k
AI Agents: How Do They Work and How to Build Them @ Shift 2025
slobodan
0
100
奥深くて厄介な「改行」と仲良くなる20分
oguemon
1
560
AIでLINEスタンプを作ってみた
eycjur
1
230
Design Foundational Data Engineering Observability
sucitw
3
200
Swift Updates - Learn Languages 2025
koher
2
510
時間軸から考えるTerraformを使う理由と留意点
fufuhu
16
4.8k
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
930
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Why Our Code Smells
bkeepers
PRO
339
57k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Building an army of robots
kneath
306
46k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
810
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
Making Projects Easy
brettharned
117
6.4k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Designing for Performance
lara
610
69k
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!