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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
iancanderson
April 04, 2013
Programming
85
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
210
Other Decks in Programming
See All in Programming
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
110
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
190
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
790
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
350
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
280
yield再入門 #phpcon
o0h
PRO
0
840
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.7k
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.7k
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
2.8k
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
710
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
290
メールのエイリアス機能を履き違えない
isshinfunada
0
160
Featured
See All Featured
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Accessibility Awareness
sabderemane
1
170
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
350
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
600
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Speed Design
sergeychernyshev
33
2k
GitHub's CSS Performance
jonrohan
1033
470k
Making Projects Easy
brettharned
120
6.7k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
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!