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.3 のてざわり
Search
Kunihiko Ito
January 30, 2016
Programming
2
430
Ruby 2.3 のてざわり
富山合同勉強会2016 ~ぶりしゃぶ会~ での発表資料です。
Kunihiko Ito
January 30, 2016
Tweet
Share
More Decks by Kunihiko Ito
See All by Kunihiko Ito
データでふりかえるToyama.rb #100 2024
kunitoo
0
15
富山Ruby会議01 をふりかえる
kunitoo
0
25
Using Ractor
kunitoo
0
120
introduction neo4j
kunitoo
0
120
vim operation and my hotkey
kunitoo
0
130
Introduction Neo4j oblove calendar
kunitoo
0
1.3k
アジャイルソフトウェア開発の概要と現場での実践
kunitoo
0
1.9k
Introduction of neo4j
kunitoo
0
1.9k
てさぐれ!受託もの
kunitoo
1
540
Other Decks in Programming
See All in Programming
「影響が少ない」を自分の目でみてみる
o0h
PRO
2
1.1k
個人開発の学生アプリが企業譲渡されるまで
akidon0000
0
520
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
4
1.1k
Empowering Developers with HTML-Aware ERB Tooling @ RubyKaigi 2025, Matsuyama, Ehime
marcoroth
2
740
Module Boundaries and Architecture with Forensic Analysis @NxSummit Amsterdam 2025
manfredsteyer
PRO
0
100
AIコーディングエージェントを 「使いこなす」ための実践知と現在地 in ログラス / How to Use AI Coding Agent in Loglass
rkaga
3
520
[NG India] Event-Based State Management with NgRx SignalStore
markostanimirovic
1
160
Being an ethical software engineer
xgouchet
PRO
0
210
Vibe Codingをせずに Clineを使っている
watany
17
6.3k
RuboCop: Modularity and AST Insights
koic
2
1.4k
gen_statem - OTP's Unsung Hero
whatyouhide
1
210
AIコーディングの理想と現実
tomohisa
25
32k
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
13
1.4k
Designing Experiences People Love
moore
141
24k
Code Reviewing Like a Champion
maltzj
522
40k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.4k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.4k
Adopting Sorbet at Scale
ufuk
76
9.3k
The Cult of Friendly URLs
andyhume
78
6.3k
Making the Leap to Tech Lead
cromwellryan
133
9.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
29
5.6k
Transcript
Ruby 2.3 のてざ わり 新機能と使いどころ Kunihiko Ito ESM 富山合同勉強会2016 2016-01-30
はじめ まして
p self
p self 名前: 伊藤 邦彦 出身: 富山 在住: 東京 所属:
ESM アジャイル事業部 仕事: [Rails, neo4j]
@kunitoo
@kunitoo
From Java To Ruby 変わったこと IDE を使わなくなった REPLで試しながら書くようにな った
プログラムを書くときにして いること rails console を立ちあげる console に書いて試していく 1. ファイルに書き移していく 2.
複雑になったらテストを書く 3.
今日伝え たいこと
Ruby お もしろい
Ruby を 触ってみた い
今日の内容 昨年の12月25日にリリースされた Ruby 2.3 の新機能とその使いどこ ろを紹介します
None
Ruby 2.3 新機 能
すぐに使える safe navigation operator SQUIGGLY HEREDOC
使う機会が増えそう #dig Enumerable#grep_v
その他 the did_you_mean gem NameError#receiver Hash#to_proc
すぐに使える safe navigation operator SQUIGGLY HEREDOC
safe navigation operator 別名 lonely operator ‐ ぼっち演算子 ‐ Active
Support の try! と同様 の挙動
safe navigation operator obj = nil obj.hoge #=> NoMethodError: #
undefined method `hoge' for nil:NilClass obj&.hoge #=> nil
&.
&. の使いどころ ユーザーがログインしていないとき current_user = nil current_user&.name #=> nil current_user.try!(:name)
#=> nil currnet_user = User.find_by(name: 'kunitoo') current_user&.name #=> 'kunitoo' current_user.try!(:name) #=> 'kunitoo'
SQUIGGLY HEREDOC ヒアドキュメント内のインデントを 取り除く `<<~` リテラルです Active Support の strip_heredoc
と同様の動きをし ます
SQUIGGLY HEREDOC # 通常のヒアドキュメント <<-HEREDOC hoge fuga HEREDOC #=> "
hoge\n\n fuga\n" # SQUIGGLY HEREDOC <<~SQUIGGLY_HEREDOC hoge fuga SQUIGGLY_HEREDOC #=> "hoge\n\nfuga\n"
SQUIGGLY HEREDOC # strip_heredoc <<-HEREDOC.strip_heredoc hoge fuga HEREDOC #=> "hoge\n\nfuga\n"
# SQUIGGLY HEREDOC <<~SQUIGGLY_HEREDOC hoge fuga SQUIGGLY_HEREDOC #=> "hoge\n\nfuga\n"
SQUIGGLY_HEREDOC の 使いどころ 簡易なメッセージやQueryのテン プレートとして使う
SQUIGGLY_HEREDOC の 使いどころ def calc_billin query = <<~SQL INSERT INTO
bills (name, total) SELECT name, sum(amount) AS total FROM orders JOIN ... WHERE ... SQL ActiveRecord::Base.connection.execute(query) end
使う機会が増えそう #dig Enumerable#grep_v
#dig 追加されたクラス Array ‐ Hash ‐ Struct ‐ OpenStruct ‐
深い階層にある値を取得するこ とができる
#dig a = [[1, 2], [3, 4]] a.dig(0, 1) #=>
2 a.dig(1, 2) #=> nil h = {foo: {bar: 1}}} h.dig(:foo, :bar) #=> 1
#dig dig メソッドを持つオジェクトであ れば、交ざっていても使えます user = { user: { address:
[ {name: '富山市', ruby: 'とやまし'}, {name: '呉羽町', ruby: 'くれはまち'} ] } } user.dig(:user, :address, 1, :name) #=> "呉羽町" user.dig(:user, :address, 2, :name) #=> nil
#dig の使いどころ JSON の値の取得 Hash の値に Array がある場合 request paramter
user = JSON.parse(request.body)
#dig の使いどころ (0..1).map {|index| user.dig(:user, :address, index, :name) } #=>
["呉羽町", nil]
Enumerable#grep_v Enumerable#grep のマッチの条 件を逆にして、pattern === item が成立 しない要素を全て含ん だ配列を返します (1..10).grep_v
2..5 # => [1, 6, 7, 8, 9, 10]
正規表現クイズ Bob, John, Jahn の中から Jo から 始まる名前以外を抽出するには? ^[^Jo] 1.
^[^J][^o] 2. ^(?!Jo) 3.
こたえ 3. ^(?!Jo) names.grep /^[^Jo]/ #=> ['Bob'] names.grep /^[^J][^o]/ #=>
[] names.grep /^(?!Jo)/ #=> ["Bob", "Jahn"]
Enumerable#grep_v の 使いどころ Enumerable#grep では逆の条件 が書きづらいときや型情報を使うと き ['Bob', 'John', 'Jahn'].grep_v
/^Jo/ #=> ["Bob", "Jahn"] [1, '1', 1.0].grep_v String #=> [1, 1.0]
the did_you_mean gem did_you_mean gem がバンドル されるようになりました。 NameError と NoMethodError
の発生時、デバッグを容易にするた め、正しい名前と思われる候補を合 わせて表示します。
the did_you_mean gem の使いどころ irb や rails console でお世話にな ります。自信のないスペルでも調べ
なくてもよくなります。
the did_you_mean gem の使いどころ 'hello'.revarse # => NoMethodError: undefined method
`revarse' for "hello":String # Did you mean? reverse # reverse! 'str'.encodeing # => NoMethodError: undefined method `encodeing' for "str":String # Did you mean? encoding # encode # encode!
NameError#receiver NameError が発生した時のレシー バオブジェクトを返します begin 'abc'.foo rescue => e p
e.receiver end #=> 'abc'
NameError#receiver の 使いどころ module DidYouMean class VariableNameChecker ... snip ...
def initialize(exception) @name = exception.name.to_s.tr("@", "") @lvar_names = exception.local_variables receiver = exception.receiver @method_names = receiver.methods + receiver.private_methods @ivar_names = receiver.instance_variables @cvar_names = receiver.class.class_variables @cvar_names += receiver.class_variables if receiver.kind_of?(Module) end end end
NameError#receiver の 使いどころ デバッグするときに呼び出し元のオ ブジェクトそのものを取得できる
Hash#to_proc self に対応する Proc オブジェク トを返します。 [1, 2, 3].map(&h) #
=> [10, 20, 30]
Hash#to_proc の使いど ころ おもいつきません...
その他 frozen string literal String#+@, String#-@
まとめ Rails に存在した &. や HEREDOC はすぐに使えそう #dig や grep_v
は意識していれ ば、使えるところがありそう Hash#to_proc はだれか使いど ころおしえてください
Enjoy Ruby