検索結果
先頭3件
-
Date
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (35119.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" が出力される
c......g 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 (29107.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: ...2......th}"
end
#=> "working day in month 10" が出力される
# クラスのチェックと組み合わせて利用することもできます
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_primitiv... -
パターンマッチ (90.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/Find/Hash パターンは、配列やハッシュのリテラルの他に、deconstruct メソッド(Array/Find パターン) あるいは deconstruct_keys メソッド(Hash パターン) を定義......hed"
end
#=> "matched: 1"
//}
以下のクラスは deconstruct や deconstruct_keys を実装しています。
MatchData#deconstruct
MatchData#deconstruct_keys
Time#deconstruct_keys
Date#deconstruct_keys
DateTime#deconstruct_keys
===[a:guard_clauses] ガード節
if を使って、パターン...