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
350
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
81
introduction neo4j
kunitoo
0
87
vim operation and my hotkey
kunitoo
0
120
Introduction Neo4j oblove calendar
kunitoo
0
1.2k
アジャイルソフトウェア開発の概要と現場での実践
kunitoo
0
1.7k
Introduction of neo4j
kunitoo
0
1.8k
てさぐれ!受託もの
kunitoo
1
500
Hypermicrodata Client
kunitoo
0
52
1年かけてgemを1つ作りました
kunitoo
3
1.3k
Other Decks in Programming
See All in Programming
SIMD Parallel Programming with the Vector API
josepaumard
0
120
新宿ダンジョンを可視化してみた
satoshi7190
2
240
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
300
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
Designing for tomorrow's programming workflows
honnibal
PRO
2
120
Milestoner
bkuhlmann
1
410
Rethinking UI building strategies @ SFI 2024
letelete
0
270
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
100
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
640
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
130
Featured
See All Featured
KATA
mclloyd
15
12k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
Building Effective Engineering Teams - LeadDev
addyosmani
28
1.8k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
BBQ
matthewcrist
80
8.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
187
16k
The Cult of Friendly URLs
andyhume
74
5.7k
Visualization
eitanlees
136
14k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Building Better People: How to give real-time feedback that sticks.
wjessup
355
18k
Code Review Best Practice
trishagee
55
15k
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