^ で変数を参照する必要があった 2 range = Time.new(2010)..Time.new(2020) 3 4 case data 5 in { created_at: ^range } 6 # data.created_at が range 内であればここが呼ばれる 7 end ` ` ` `
^ で変数を参照する必要があった 2 range = Time.new(2010)..Time.new(2020) 3 4 case data 5 in { created_at: ^range } 6 # data.created_at が range 内であればここが呼ばれる 7 end Ruby 3.1 からは以下のように直接式を書くことができる 1 # ^ で直接式を書くことができるようになった 2 case data 3 in { created_at: ^(Time.new(2010)..Time.new(2020)) } 4 # data.created_at が range 内であればここが呼ばれる 5 end ` ` ` `
1 user = { name: "homu", age: 14 } 2 3 # 3.0 : warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby! 4 # 3.1 : no warning 5 user => { name:, age: } 6 7 p name # = "homu" 8 p age # = 14 9 10 11 # 3.0 : warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby! 12 # 3.1 : no warning 13 if user in { name: String, age: (..20) } 14 puts "OK" 15 end
3 module M1 4 def hoge; end 5 6 refine X do 7 include M1 # X の継承リストに追加される 8 end 9 end 10 11 module M2 12 def foo; end 13 14 refine X do 15 # M2 のメソッドが Refinements オブジェクトに直接追加される 16 import_methods M2 17 end 18 end 19 using M1; using M2 20 21 p X.instance_method(:hoge).owner # => M1 22 p X.instance_method(:foo).owner #=> #<refinement:X@M2> ` ` ` `
2 class B < A; end 3 class C < B; end 4 5 # クラスがどのクラスで継承されているのかを返す 6 p A.descendants #=> [B, C] 7 p B.descendants #=> [C] 8 p C.descendants #=> [] ` ` ` `
2 class B < A; end 3 class C < B; end 4 5 # クラスがどのクラスで継承されているのかを返す 6 p A.descendants #=> [B, C] 7 p B.descendants #=> [C] 8 p C.descendants #=> [] Class#subclass は直接継承されているクラスの一覧を取得するメソッド 1 class A; end 2 class B < A; end 3 class C < B; end 4 class D < A; end 5 6 A.subclasses #=> [D, B] 7 B.subclasses #=> [C] 8 C.subclasses #=> [] ` ` ` ` ` `