別のキーワード
種類
- インスタンスメソッド (50)
- 文書 (12)
キーワード
- captures (2)
-
deconstruct
_ keys (20) -
named
_ captures (2) -
to
_ a (6) - values (6)
- パターンマッチ (12)
検索結果
先頭5件
-
Data
# deconstruct -> [object] (18113.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 "... -
CSV
:: Row # deconstruct -> [object] (18101.0) -
パターンマッチに使用する行の値の配列を返します。
パターンマッチに使用する行の値の配列を返します。
//emlist[例][ruby]{
require "csv"
row = CSV::Row.new(["header1", "header2", "header3"], [1, 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 ... -
Struct
# deconstruct -> [object] (18101.0) -
構造体のメンバの値を配列にいれて返します。
構造体のメンバの値を配列にいれて返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_a
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してくだ... -
MatchData
# deconstruct -> [String] (15101.0) -
$1, $2, ... を格納した配列を返します。
$1, $2, ... を格納した配列を返します。
MatchData#to_a と異なり $& を要素に含みません。
グループにマッチした部分文字列がなければ対応する要素は nil になります。
//emlist[例][ruby]{
/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a # => ["foobar", "foo", "bar", nil]
p $~.captures # => ["foo", "bar", nil]
//}
@see MatchData#to_a, MatchData#named_captures, d... -
Data
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6119.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 kilometers away" が表示さ... -
MatchData
# deconstruct _ keys(array _ of _ names) -> Hash (6119.0) -
引数で指定された名前の名前付きキャプチャを Hash で返します。
...。
//emlist[例][ruby]{
m = /(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})/.match("18:37:22")
m.deconstruct_keys([:hours, :minutes]) # => {:hours => "18", :minutes => "37"}
m.deconstruct_keys(nil) # => {:hours => "18", :minutes => "37", :seconds => "22"}
# 名前付きキャプチャが定......義されていなかった場合は空のハッシュを返す
m = /(\d{2}):(\d{2}):(\d{2})/.match("18:37:22")
m.deconstruct_keys(nil) # => {}
//}
@see d:spec/pattern_matching#matching_non_primitive_objects... -
Struct
# deconstruct _ keys(array _ of _ names) -> Hash (6113.0) -
self のメンバの名前と値の組を Hash で返します。
...omer.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 # => {:name=>"Joseph Smith, Jr.", :address=>"123... -
Date
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6107.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 d
in year: ...202... -
DateTime
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6107.0) -
パターンマッチに使用する名前と値の Hash を返します。
...をパターンマッチに使用します。
//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
puts "too old"
in month: ..9... -
Time
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6107.0) -
パターンマッチに使用する名前と値の Hash を返します。
...は全てをパターンマッチに使用します。
//emlist[例][ruby]{
t = Time.utc(2022, 10, 5, 21, 25, 30)
if t in wday: 3, day: ..7 # deconstruct_keys が使われます
puts "first Wednesday of the month"
end
#=> "first Wednesday of the month" が出力される
case t
in year: ...2022... -
CSV
:: Row # deconstruct _ keys(keys) -> Hash (6101.0) -
パターンマッチに使用するヘッダの名前と値の Hash を返します。
パターンマッチに使用するヘッダの名前と値の Hash を返します。
このメソッドはヘッダ名の型をシンボルに変換しないため、ヘッダ名が文字列かつ Hash パターン でパターンマッチしたい場合はキーをシンボルに変換する必要があります。
@param keys パターンマッチに使用するヘッダの名前の配列を指定します。nil の場合は全てをパターンマッチに使用します。
//emlist[例][ruby]{
require "csv"
row = CSV::Row.new([:header1, :header2, :header3], [1, 2, 3])
case row
in { hea... -
Struct
# to _ a -> [object] (3001.0) -
構造体のメンバの値を配列にいれて返します。
構造体のメンバの値を配列にいれて返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_a
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してくだ... -
Struct
# values -> [object] (3001.0) -
構造体のメンバの値を配列にいれて返します。
構造体のメンバの値を配列にいれて返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_a
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してくだ... -
パターンマッチ (144.0)
-
パターンマッチ * patterns * variable_binding * variable_pinning * matching_non_primitive_objects * guard_clauses * current_feature_status * pattern_syntax * some_undefined_behavior_examples
...ンをネストさせることができます。
Array パターン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。
Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash パ......なオブジェクトのマッチ: deconstruct メソッドと deconstruct_keys メソッド
既に述べたように、Array/Hash パターンは、配列やハッシュのリテラルの他に、deconstruct メソッド(Array パターン) あるいは deconstruct_keys メソッド(Hash パター......ます。
//emlist[][ruby]{
class Point
def initialize(x, y)
@x, @y = 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,......ることができます。
Array パターン と Find パターン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。
Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash......ブジェクトのマッチ: deconstruct メソッドと deconstruct_keys メソッド
既に述べたように、Array/Find/Hash パターンは、配列やハッシュのリテラルの他に、deconstruct メソッド(Array/Find パターン) あるいは deconstruct_keys メソッド(Hash パ... -
MatchData
# named _ captures -> Hash (6.0) -
名前付きキャプチャをHashで返します。
..."a" => "0", "b" => "1"}
m = /(?<a>.)(?<b>.)?/.match("0")
m.named_captures # => {"a" => "0", "b" => nil}
m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures # => {"a" => "1"}
m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}
//}
@see MatchData#captures, MatchData#deconstruct_keys... -
MatchData
# named _ captures(symbolize _ names: false) -> Hash (6.0) -
名前付きキャプチャをHashで返します。
...", "b" => nil}
m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures # => {"a" => "1"}
m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}
m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures(symbolize_names: true) #=> {:a => "1"}
//}
@see MatchData#captures, MatchData#deconstruct_keys... -
MatchData
# captures -> [String] (1.0) -
$1, $2, ... を格納した配列を返します。
$1, $2, ... を格納した配列を返します。
MatchData#to_a と異なり $& を要素に含みません。
グループにマッチした部分文字列がなければ対応する要素は nil になります。
//emlist[例][ruby]{
/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a # => ["foobar", "foo", "bar", nil]
p $~.captures # => ["foo", "bar", nil]
//}
@see MatchData#to_a, MatchData#named_captures, d...