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
290
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
57
introduction neo4j
kunitoo
0
39
vim operation and my hotkey
kunitoo
0
100
Introduction Neo4j oblove calendar
kunitoo
0
1k
アジャイルソフトウェア開発の概要と現場での実践
kunitoo
0
1.5k
Introduction of neo4j
kunitoo
0
1.8k
てさぐれ!受託もの
kunitoo
1
480
Hypermicrodata Client
kunitoo
0
45
1年かけてgemを1つ作りました
kunitoo
3
1.1k
Other Decks in Programming
See All in Programming
Cloudflare WorkersでGoを動かすライブラリを作っている話
syumai
1
330
Micro Frontends with Module Federation @MicroFrontend Summit 2023
manfredsteyer
PRO
0
640
Listかもしれない
irof
1
290
Remote SSHで行うVS Codeリモートホスト開発とトラブルシューティング
smt7174
1
520
Amebaブログの会員画面システム刷新の道程
ryotasugawara
1
250
なぜRubyコミュニティにコミットするのか?
luccafort
0
330
Swift Expression Macros: a practical introduction
kishikawakatsumi
2
740
OSC大阪 パスワード認証は人類には早すぎる ~ IDaaSを使ったソーシャルログインのすすめ ~
authyasan
7
1.5k
データドリブンな組織の不正検知
fkubota
0
310
爆速の日経電子版開発の今
shinyaigeek
2
660
Next.js 13 Layout / Streaming SSR 仕組み解説
sumiren
0
160
tidy_rpart
bk_18
0
610
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
198
18k
GitHub's CSS Performance
jonrohan
1020
430k
The Pragmatic Product Professional
lauravandoore
21
3.5k
Embracing the Ebb and Flow
colly
75
3.6k
Why You Should Never Use an ORM
jnunemaker
PRO
49
7.9k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
657
120k
Designing for Performance
lara
600
65k
For a Future-Friendly Web
brad_frost
166
7.8k
Keith and Marios Guide to Fast Websites
keithpitt
407
21k
Principles of Awesome APIs and How to Build Them.
keavy
117
15k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
349
27k
Infographics Made Easy
chrislema
235
17k
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#
[email protected]
, String#
[email protected]
まとめ Rails に存在した &. や HEREDOC はすぐに使えそう #dig や grep_v
は意識していれ ば、使えるところがありそう Hash#to_proc はだれか使いど ころおしえてください
Enjoy Ruby