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
84
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
ふつうのFeature Flag実践入門
irof
7
3.7k
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.6k
New "Type" system on PicoRuby
pocke
1
810
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
160
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.3k
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
20
6.4k
AutonomyとControlのあいだ:Graflowで記述するAIエージェント協調
myui
0
120
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
490
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.6k
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
210
The Limits of Empathy - UXLibs8
cassininazir
1
350
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
330
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
330
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
22k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Ruling the World: When Life Gets Gamed
codingconduct
0
250
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
150
New Earth Scene 8
popppiees
3
2.3k
Being A Developer After 40
akosma
91
590k
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!