30件ヒット
[1-30件を表示]
(0.028秒)
種類
- インスタンスメソッド (18)
- 文書 (12)
キーワード
-
deconstruct
_ keys (12) - パターンマッチ (12)
検索結果
先頭5件
-
Data
# deconstruct -> [object] (18125.0) -
self のメンバの値を配列で返します。
...(10, 'km')
distance.deconstruct # => [10, "km"]
//}
このメソッドは以下のようにパターンマッチで利用されます。
//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)
distance = Measure.new(10, 'km')
case distance
in n, 'km' # 裏側で #deconstruct を呼ぶ
puts "......is #{n} kilometers away"
else
puts "Don't know how to handle it"
end
# "It is 10 kilometers away" が表示される
# 以下のようにも書ける
case distance
in Measure(n, 'km')
puts "It is #{n} kilometers away"
# ...
end
//}
[注意] 本メソッドの記述は Data のサブクラス... -
CSV
:: Row # deconstruct -> [object] (18107.0) -
パターンマッチに使用する行の値の配列を返します。
..., 2, 3])
case row
in [2.., 2.., 2..]
puts "all 2 or more"
in [...2, 2.., 2..]
puts "first column is less than 2, and rest columns are 2 or more"
end
#=> "first column is less than 2, and rest columns are 2 or more" が出力される
//}
@see d:spec/pattern_matching#matching_non_primitive_objec... -
Data
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6131.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}
//}
このメソッドは以下のようにパターンマッチで利用......#deconstruct_keys を呼ぶ
puts "It is #{amount} kilometers away"
else
puts "Don't know how to handle it"
end
# "It is 10 kilometers away" が表示される
# 以下のようにも書ける
case distance
in Measure(amount:, unit: 'km')
puts "It is #{amount} kilometers away"
# ...
end
//}... -
Date
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6125.0) -
パターンマッチに使用する名前と値の Hash を返します。
...3, day: ..7 # deconstruct_keys が使われます
puts "first Wednesday of the month"
end
#=> "first Wednesday of the month" が出力される
case d
in year: ...2022
puts "too old"
in month: ..9
puts "quarter 1-3"
in wday: 1..5, month:
puts "working day in month #{month}"
end
#=> "working......day in month 10" が出力される
# クラスのチェックと組み合わせて利用することもできます
if d in Date(wday: 3, day: ..7)
puts "first Wednesday of the month"
end
//}
@see d:spec/pattern_matching#matching_non_primitive_objects... -
DateTime
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6125.0) -
パターンマッチに使用する名前と値の Hash を返します。
...in wday: 1..5, hour: 10..18 # deconstruct_keys が使われます
puts "Working time"
end
#=> "Working time" が出力される
case dt
in year: ...2022
puts "too old"
in month: ..9
puts "quarter 1-3"
in wday: 1..5, month:
puts "working day in month #{month}"
end
#=> "working day in month 1......0" が出力される
# クラスのチェックと組み合わせて利用することもできます
if dt in DateTime(wday: 1..5, hour: 10..18, day: ..7)
puts "Working time, first week of the month"
end
//}
@see d:spec/pattern_matching#matching_non_primitive_objects... -
Time
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6125.0) -
パターンマッチに使用する名前と値の Hash を返します。
...3, day: ..7 # deconstruct_keys が使われます
puts "first Wednesday of the month"
end
#=> "first Wednesday of the month" が出力される
case t
in year: ...2022
puts "too old"
in month: ..9
puts "quarter 1-3"
in wday: 1..5, month:
puts "working day in month #{month}"
end
#=> "working......day in month 10" が出力される
# クラスのチェックと組み合わせて利用することもできます
if t in Time(wday: 3, day: ..7)
puts "first Wednesday of the month"
end
//}
@see d:spec/pattern_matching#matching_non_primitive_objects... -
CSV
:: Row # deconstruct _ keys(keys) -> Hash (6107.0) -
パターンマッチに使用するヘッダの名前と値の Hash を返します。
...: 2.. }
puts "all 2 or more"
in { header1: ...2, header2: 2.., header3: 2.. }
puts "first column is less than 2, and rest columns are 2 or more"
end
#=> "first column is less than 2, and rest columns are 2 or more" が出力される
//}
@see d:spec/pattern_matching#matching_non_primitive_objec... -
パターンマッチ (420.0)
-
パターンマッチ * patterns * variable_binding * variable_pinning * matching_non_primitive_objects * guard_clauses * current_feature_status * pattern_syntax * some_undefined_behavior_examples
...case 文の中で利用できます。
case <expression>
in <pattern1>
...
in <pattern2>
...
in <pattern3>
...
else
...
end
in 節と when 節は1つの case 式の中に混ぜて書くことはできません。
case/in 式は 「網羅的」 です。もし case......ts "Connect with user '#{user}'"
in connection: {username: }
puts "Connect with user '#{username}'"
else
puts "Unrecognized structure of config"
end
# "Connect with user 'admin'" と出力
//}
一方、『in』 文は、期待されるデータ構造があらかじめ分かっている場合に......ンをネストさせることができます。
Array パターン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。
Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash パ......を用いて実装されています。
case <expression>
in <pattern1>
...
in <pattern2>
...
in <pattern3>
...
else
...
end
in 節と when 節は1つの case 式の中に混ぜて書くことはできません。
『=>』 演算子と in 演算子で、単体の式で......ts "Connect with user '#{user}'"
in connection: {username: }
puts "Connect with user '#{username}'"
else
puts "Unrecognized structure of config"
end
# "Connect with user 'admin'" と出力
//}
一方、『=>』 演算子は、期待されるデータ構造があらかじめ分かっている場......ることができます。
Array パターン と Find パターン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。
Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash...