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
80
2
Share
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
210
Other Decks in Programming
See All in Programming
Don't Prompt Harder, Structure Better
kitasuke
0
700
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
200
今からFlash開発できるわけないじゃん、ムリムリ! (※ムリじゃなかった!?)
arkw
0
190
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.8k
Make GenAI Production-Ready with Kubernetes Patterns
bibryam
0
110
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
2.9k
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.4k
TiDBのアーキテクチャから学ぶ分散システム入門 〜MySQL互換のNewSQLは何を解決するのか〜 / tidb-architecture-study
dznbk
1
160
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
170
PHP で mp3 プレイヤーを実装しよう
m3m0r7
PRO
0
250
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.3k
KagglerがMixSeekを触ってみた
morim
0
370
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
220
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
Skip the Path - Find Your Career Trail
mkilby
1
100
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
54k
The Curse of the Amulet
leimatthew05
1
11k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
260
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
510
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
330
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
360
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!