シュッとふりかえる Ruby 2.0 以降
-
Upload
tomohiro-nishimura -
Category
Software
-
view
106 -
download
2
Transcript of シュッとふりかえる Ruby 2.0 以降
シュッとふりかえる Ruby 2.0 以降2015/02/20 第65回 Ruby関西 勉強会
西村 友裕 a.k.a Sixeight
Sixeight (@tomohi_ro)
近況:Perl
ざっくりと変更点を ふりかえります。
ただし、理解出来たところや、興味でフィルタしています。
2013年2月25日 (Ruby生誕20周年)
Ruby 2.0
キーワード引数
!def meth(foo: “bar”, hoge: “piyo”) [foo, hoge] end !p meth(foo: “cat”) #=> [“cat”, “piyo”]
Refinements
• (誤解を恐れずにいうと)特定のスコープの中でのみ有効なメソッドを定義する方法
• Experimental な機能として入りました
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
デフォルトエンコーディング が UTF-8 になった
• # encoding: utf-8 が不要になった!!!!
Enumerable#lazy
users = [ 5億件のデータ ] p users.each.lazy. map {|u| u.newbie? }. map {|u| u.rubyist? }. map {|u| u.age < 20 }. to_a
その他• %i
• __dir__
• Hash(), nil#to_h, Hash#to_h
• const_get(“Foo::Bar::Baz”)
• Array#bsearch
• TracePoint
• gem ̶no-document
2013年12月25日
Ruby 2.1
RGenGC になった
• パフォーマンス向上!
キーワード引数の拡張!def meth(foo:, hoge:) [foo, hoge] end !p meth(foo: “cat”) #=> `meth': missing keywords: hoge (ArgumentError)
メソッド定義が シンボルを返すように
class A private def method # ... end end
つまり def hoge; end #=> :hoge
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>"
ローカル変数を取得したり設定できる = 予約語もキーワード引数として使える
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 族の対応は入らなかった
Module#include/prepend が public なメソッドになった
A.include AwesomeModule A.prepend AwesomeModule
A.__send__(:include, AwesomeModule) A.__send__(:prepend, AwesomeModule)
freeze
• 全ての Symbol がフリーズされるようになった
• フリーズされた文字列が同じ object_id を返すようになった
• ハッシュのキーに文字列を指定するとフリーズされるようになった = Symbol と同じ扱い
その他• 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 ...
2014年12月25日
Ruby 2.2
{“symbol-key”: “value”}
{:"symbol-key" => "value"}
{“symbol-key": "value"}
GC
• インクリメンタル GC の導入
• シンボルが GC の対象になった
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>
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 で呼び出せるメソッドを取得出来るようになった
Method#curry
def add(a, b) a + b end !add_ten = method(:add).curry.(10) p add_ten.(5) #=> 15
Proc#curry と同じことが出来るようになった
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
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)
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
注) 次のバージョンから <=> の例外を隠蔽してくれなくなります。
その他• 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 が消えてる
• 細かい変更がたくさんある
ご清聴ありがとう ございました。