シュッとふりかえる Ruby 2.0 以降

34
シュッとふりかえる Ruby 2.0 以降 2015/02/20 第65回 Ruby関西 勉強会 西村 友裕 a.k.a Sixeight

Transcript of シュッとふりかえる Ruby 2.0 以降

Page 1: シュッとふりかえる Ruby 2.0 以降

シュッとふりかえる Ruby 2.0 以降2015/02/20 第65回 Ruby関西 勉強会

西村 友裕 a.k.a Sixeight

Page 2: シュッとふりかえる Ruby 2.0 以降

Sixeight (@tomohi_ro)

近況:Perl

Page 3: シュッとふりかえる Ruby 2.0 以降

ざっくりと変更点を ふりかえります。

Page 4: シュッとふりかえる Ruby 2.0 以降

ただし、理解出来たところや、興味でフィルタしています。

Page 5: シュッとふりかえる Ruby 2.0 以降

2013年2月25日 (Ruby生誕20周年)

Page 6: シュッとふりかえる Ruby 2.0 以降

Ruby 2.0

Page 7: シュッとふりかえる Ruby 2.0 以降

キーワード引数

!def meth(foo: “bar”, hoge: “piyo”) [foo, hoge] end !p meth(foo: “cat”) #=> [“cat”, “piyo”]

Page 8: シュッとふりかえる Ruby 2.0 以降

Refinements

• (誤解を恐れずにいうと)特定のスコープの中でのみ有効なメソッドを定義する方法

• Experimental な機能として入りました

Page 9: シュッとふりかえる Ruby 2.0 以降

Module#prependmodule Awesome def greet "awesome " + super end end !class Person prepend Awesome def greet "hi!" end end !puts Person.new.greet #=> awesome hi!

• 継承の先頭に挿し込める機能 !

• include Awesome • Person -> Awesome

• prepend Awesome • Awesome -> Person

!

• R.I.P #alias_method_chain

Page 10: シュッとふりかえる Ruby 2.0 以降

デフォルトエンコーディング が UTF-8 になった

• # encoding: utf-8 が不要になった!!!!

Page 11: シュッとふりかえる Ruby 2.0 以降

Enumerable#lazy

users = [ 5億件のデータ ] p users.each.lazy. map {|u| u.newbie? }. map {|u| u.rubyist? }. map {|u| u.age < 20 }. to_a

Page 12: シュッとふりかえる Ruby 2.0 以降

その他• %i

• __dir__

• Hash(), nil#to_h, Hash#to_h

• const_get(“Foo::Bar::Baz”)

• Array#bsearch

• TracePoint

• gem ̶no-document

Page 13: シュッとふりかえる Ruby 2.0 以降

2013年12月25日

Page 14: シュッとふりかえる Ruby 2.0 以降

Ruby 2.1

Page 15: シュッとふりかえる Ruby 2.0 以降

RGenGC になった

• パフォーマンス向上!

Page 16: シュッとふりかえる Ruby 2.0 以降

キーワード引数の拡張!def meth(foo:, hoge:) [foo, hoge] end !p meth(foo: “cat”) #=> `meth': missing keywords: hoge (ArgumentError)

Page 17: シュッとふりかえる Ruby 2.0 以降

メソッド定義が シンボルを返すように

class A private def method # ... end end

つまり def hoge; end #=> :hoge

Page 18: シュッとふりかえる Ruby 2.0 以降

Binding#local_variable_(get|set)

def span(body, class: "") html_class = binding.local_variable_get(:class) %q!<span class="%s">%s</span>! % [html_class, body] end !p span("hoge", class: “right”) !#=> "<span class=\"right\">hoge</span>"

ローカル変数を取得したり設定できる = 予約語もキーワード引数として使える

Page 19: シュッとふりかえる Ruby 2.0 以降

Refinementsmodule Bangable refine String do def ! self + "!" end end end !class Person using Bangable ! def greet(name) puts "Hi #{name}".! end end !Person.new.greet("cat") #=> Hi cat! !puts "Hi dog".! #=> false

• Experimental が取れた! !• クラス/モジュール のスコープにも有効に

• eval 族の対応は入らなかった

Page 20: シュッとふりかえる Ruby 2.0 以降

Module#include/prepend が public なメソッドになった

A.include AwesomeModule A.prepend AwesomeModule

A.__send__(:include, AwesomeModule) A.__send__(:prepend, AwesomeModule)

Page 21: シュッとふりかえる Ruby 2.0 以降

freeze

• 全ての Symbol がフリーズされるようになった

• フリーズされた文字列が同じ object_id を返すようになった

• ハッシュのキーに文字列を指定するとフリーズされるようになった = Symbol と同じ扱い

Page 22: シュッとふりかえる Ruby 2.0 以降

その他• Array#to_h

• 0.5r, 0.5i, 0.5ri

• Exception#cause

• Kernel#singleton_method, Kernel#singleton_method?

• String#scrub

• eval 族が環境を引き継がなくなった

• lambda Proc からの return の挙動の統一

• and more ...

Page 23: シュッとふりかえる Ruby 2.0 以降

2014年12月25日

Page 24: シュッとふりかえる Ruby 2.0 以降

Ruby 2.2

Page 25: シュッとふりかえる Ruby 2.0 以降

{“symbol-key”: “value”}

{:"symbol-key" => "value"}

{“symbol-key": "value"}

Page 26: シュッとふりかえる Ruby 2.0 以降

GC

• インクリメンタル GC の導入

• シンボルが GC の対象になった

Page 27: シュッとふりかえる Ruby 2.0 以降

Binding• Binding#local_variables

• Binding#receiver class A def context x = 1 y = 2 binding end end !a = A.new p a.context.local_variables #=> [:x, :y] p a.context.receiver #=> #<A:0x007fa450833dc0>

Page 28: シュッとふりかえる Ruby 2.0 以降

Method#super_method

class Parent def meth end end !class Child < Parent def meth end end !p Child.new.method(:meth).super_method #=> #<Method: Parent#meth>

super で呼び出せるメソッドを取得出来るようになった

Page 29: シュッとふりかえる Ruby 2.0 以降

Method#curry

def add(a, b) a + b end !add_ten = method(:add).curry.(10) p add_ten.(5) #=> 15

Proc#curry と同じことが出来るようになった

Page 30: シュッとふりかえる Ruby 2.0 以降

Enumerable• #slice_after • #slice_before の逆

• #max, #max_by, #min, #min_by • 引数を取れるようになった

counts = [1, 2, 3, 4, 5] !counts.slice_before(&:even?) #=> [[1, 2], [3, 4], [5]] !counts.max(2) #=> 5, 4 counts.min(3) #=> 1, 2, 3

Page 31: シュッとふりかえる Ruby 2.0 以降

Object#itself

state = state.presence_in(AVAILABLE_STATES) Book.public_send(state || :itself).order(desc: :price)

自分自身を返すメソッド

例えばこう使う

指定されたステートが有効なものであればその中から、無効なものであれば全体から検索する。

Book.sale.order(desc: :price) もしくは Book.order(desc: :price)

Page 32: シュッとふりかえる Ruby 2.0 以降

Comparable#==class Person include Comparable ! attr_reader :age ! def initialize(age) @age = age end ! def <=>(other) self.age <=> other.age end end !a = Person.new(20) b = Person.new(18) !p a > b #=> true !p a == nil #=> warning: Comparable#== will no more rescue exceptions of #<=> in the next release. #=> warning: Return nil in #<=> if the comparison is inappropriate or avoid such comparison. #=> false

注) 次のバージョンから <=> の例外を隠蔽してくれなくなります。

Page 33: シュッとふりかえる Ruby 2.0 以降

その他• nil/true/false が frozen に変更

• Pathname(“cute”)/“cat”/“dog”

• File#birthtime

• Float#(next|prev)_float

• Matrix#first_minor, Matrix#cofactor

• Digest#HMAC がなくなった (OpenSSL::HMAC)

• C API の Deprecated が消えてる

• 細かい変更がたくさんある

Page 34: シュッとふりかえる Ruby 2.0 以降

ご清聴ありがとう ございました。