28件ヒット
[1-28件を表示]
(0.026秒)
別のキーワード
種類
- インスタンスメソッド (16)
- 文書 (12)
検索結果
先頭5件
-
Data
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (18131.0) -
self のメンバの名前と値の組を Hash で返します。
...返します。
//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)
distance = Measure.new(10, 'km')
distance.deconstruct_keys(nil) # => {:amount=>10, :unit=>"km"}
distance.deconstruct_keys([:amount]) # => {:amount=>10}
//}
このメソッドは以下のようにパターンマッチ......で利用されます。
//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)
distance = Measure.new(10, 'km')
case distance
in amount:, unit: 'km' # 裏側で #deconstruct_keys を呼ぶ
puts "It is #{amount} kilometers away"
else
puts "Don't know how to handle it"
end
# "It is 10 kilom... -
Struct
# deconstruct _ keys(array _ of _ names) -> Hash (18131.0) -
self のメンバの名前と値の組を Hash で返します。
...= Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
h = joe.deconstruct_keys([:zip, :address])
h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
# 引数が nil の場合は全てのメンバを返します。
h = joe.deconstruct_keys(nil)
h #......345}
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。
@see d:spec/pattern_matching#matching_non_primitive_objects... -
Date
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (18113.0) -
パターンマッチに使用する名前と値の Hash を返します。
...指定します。nil の場合は全てをパターンマッチに使用します。
//emlist[例][ruby]{
d = Date.new(2022, 10, 5)
if d in wday: 3, day: ..7 # deconstruct_keys が使われます
puts "first Wednesday of the month"
end
#=> "first Wednesday of the month" が出力される
case... -
DateTime
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (18113.0) -
パターンマッチに使用する名前と値の Hash を返します。
...す。nil の場合は全てをパターンマッチに使用します。
//emlist[例][ruby]{
dt = DateTime.new(2022, 10, 5, 13, 30)
if dt in wday: 1..5, hour: 10..18 # deconstruct_keys が使われます
puts "Working time"
end
#=> "Working time" が出力される
case dt
in year: ...2022... -
CSV
:: Row # deconstruct _ keys(keys) -> Hash (18107.0) -
パターンマッチに使用するヘッダの名前と値の Hash を返します。
...配列を指定します。nil の場合は全てをパターンマッチに使用します。
//emlist[例][ruby]{
require "csv"
row = CSV::Row.new([:header1, :header2, :header3], [1, 2, 3])
case row
in { header1: 2.., header2: 2.., header3: 2.. }
puts "all 2 or more"
in { header1: ...2, header... -
パターンマッチ (114.0)
-
パターンマッチ * patterns * variable_binding * variable_pinning * matching_non_primitive_objects * guard_clauses * current_feature_status * pattern_syntax * some_undefined_behavior_examples
...ーン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。
Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash パターン で利用できるキーはシンボルのみです......トのマッチ: deconstruct メソッドと deconstruct_keys メソッド
既に述べたように、Array/Hash パターンは、配列やハッシュのリテラルの他に、deconstruct メソッド(Array パターン) あるいは deconstruct_keys メソッド(Hash パターン) を定義し......= x, y
end
def deconstruct
puts "deconstruct called"
[@x, @y]
end
def deconstruct_keys(keys)
puts "deconstruct_keys called with #{keys.inspect}"
{x: @x, y: @y}
end
end
case Point.new(1, -2)
in px, Integer # パターンと変数への束縛も動きます
"matched: #......マッチ: deconstruct メソッドと deconstruct_keys メソッド
既に述べたように、Array/Find/Hash パターンは、配列やハッシュのリテラルの他に、deconstruct メソッド(Array/Find パターン) あるいは deconstruct_keys メソッド(Hash パターン) を定義...