Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Ruby 2.3 のてざわり
Kunihiko Ito
January 30, 2016
Programming
2
260
Ruby 2.3 のてざわり
富山合同勉強会2016 ~ぶりしゃぶ会~ での発表資料です。
Kunihiko Ito
January 30, 2016
Tweet
Share
More Decks by Kunihiko Ito
See All by Kunihiko Ito
Using Ractor
kunitoo
0
50
introduction neo4j
kunitoo
0
28
vim operation and my hotkey
kunitoo
0
89
Introduction Neo4j oblove calendar
kunitoo
0
940
アジャイルソフトウェア開発の概要と現場での実践
kunitoo
0
1.4k
Introduction of neo4j
kunitoo
0
1.7k
てさぐれ!受託もの
kunitoo
1
480
Hypermicrodata Client
kunitoo
0
42
1年かけてgemを1つ作りました
kunitoo
3
1k
Other Decks in Programming
See All in Programming
シェーダー氷山発掘記
logilabo
0
140
Cross Deviceチームにおけるスマートテレビアプリ開発ってどんな感じ?
cokaholic
0
120
Angular‘s Future without NgModules: Architectures with Standalone Components @enterJS
manfredsteyer
PRO
0
200
設計ナイト2022 トランザクションスクリプト
shinpeim
11
2k
Seleniumでイキってたらサーバを絞め落としかけてた話
kenfujita
0
360
模組化的Swift架構(二) DDD速成
haifengkao
0
380
Jakarta EE 10 and Beyond
ivargrimstad
0
2k
Client-Side Field-Level Encryption for Apache Kafka Connect @ VoxxedDays Luxembourg 2022
hpgrahsl
0
110
Modern Android Developer ~ 안내서
pluu
1
610
Beyond Micro Frontends: Frontend Moduliths for the Enterprise @wad2022
manfredsteyer
PRO
0
130
io22 extended What's new in app performance
veronikapj
0
340
IE Graduation (IE の功績を讃える)
jxck
20
12k
Featured
See All Featured
Scaling GitHub
holman
451
140k
Why Our Code Smells
bkeepers
PRO
324
55k
Making the Leap to Tech Lead
cromwellryan
113
7.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
5
2.2k
A designer walks into a library…
pauljervisheath
196
16k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
12
920
How to Ace a Technical Interview
jacobian
265
21k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
Principles of Awesome APIs and How to Build Them.
keavy
113
15k
A better future with KSS
kneath
225
15k
Web development in the modern age
philhawksworth
197
9.3k
Six Lessons from altMBA
skipperchong
14
1.4k
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