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
440
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
19
富山Ruby会議01 をふりかえる
kunitoo
0
34
Using Ractor
kunitoo
0
130
introduction neo4j
kunitoo
0
130
vim operation and my hotkey
kunitoo
0
140
Introduction Neo4j oblove calendar
kunitoo
0
1.4k
アジャイルソフトウェア開発の概要と現場での実践
kunitoo
0
2k
Introduction of neo4j
kunitoo
0
1.9k
てさぐれ!受託もの
kunitoo
1
550
Other Decks in Programming
See All in Programming
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6.1k
GraphRAGの仕組みまるわかり
tosuri13
6
340
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
510
DroidKnights 2025 - 다양한 스크롤 뷰에서의 영상 재생
gaeun5744
3
260
F#で自在につくる静的ブログサイト - 関数型まつり2025
pizzacat83
0
300
カクヨムAndroidアプリのリブート
numeroanddev
0
420
AIネイティブなプロダクトをGolangで挑む取り組み
nmatsumoto4
0
100
Passkeys for Java Developers
ynojima
3
860
社内での開発コミュニティ活動とモジュラーモノリス標準化事例のご紹介/xPalette and Introduction of Modular monolith standardization
m4maruyama
1
120
A comprehensive view of refactoring
marabesi
0
480
技術懸念に立ち向かい 法改正を穏便に乗り切った話
pop_cashew
0
1.4k
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
230
Featured
See All Featured
For a Future-Friendly Web
brad_frost
179
9.8k
Gamification - CAS2011
davidbonilla
81
5.3k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
The Invisible Side of Design
smashingmag
299
51k
RailsConf 2023
tenderlove
30
1.1k
Statistics for Hackers
jakevdp
799
220k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
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