るりまサーチ

最速Rubyリファレンスマニュアル検索!
62件ヒット [1-62件を表示] (0.040秒)
トップページ > クエリ:>[x] > クエリ:deconstruct[x]

別のキーワード

  1. _builtin >
  2. bigdecimal >
  3. complex >
  4. integer >
  5. float >

ライブラリ

クラス

キーワード

検索結果

Data#deconstruct -> [object] (18213.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] (18201.0)

パターンマッチに使用する行の値の配列を返します。

...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_objects...

Struct#deconstruct -> [object] (18201.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 の下位クラスのインスタンスに対して呼...

MatchData#deconstruct -> [String] (15201.0)

$1, $2, ... を格納した配列を返します。

...ば対応する要素は 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:spec/pattern_matching#matching_non_primitive_objec...

Data#deconstruct_keys(array_of_names_or_nil) -> Hash (6219.0)

self のメンバの名前と値の組を Hash で返します。

...[例][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 (6219.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 (6213.0)

self のメンバの名前と値の組を Hash で返します。

...", 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 Maple, Anytown NC", :zip=>12345}
//}

[注...

Date#deconstruct_keys(array_of_names_or_nil) -> Hash (6207.0)

パターンマッチに使用する名前と値の Hash を返します。

...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 d...

DateTime#deconstruct_keys(array_of_names_or_nil) -> Hash (6207.0)

パターンマッチに使用する名前と値の Hash を返します。

...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 10"...

Time#deconstruct_keys(array_of_names_or_nil) -> Hash (6207.0)

パターンマッチに使用する名前と値の Hash を返します。

...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 d...

絞り込み条件を変える

CSV::Row#deconstruct_keys(keys) -> Hash (6201.0)

パターンマッチに使用するヘッダの名前と値の Hash を返します。

.... }
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_objects...

Struct#to_a -> [object] (3101.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#values -> [object] (3101.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 の下位クラスのインスタンスに対して呼...

パターンマッチ (210.0)

パターンマッチ * patterns * variable_binding * variable_pinning * matching_non_primitive_objects * guard_clauses * current_feature_status * pattern_syntax * some_undefined_behavior_examples

...in 演算子を用いて実装されており、単体の式や

<expression> in <pattern>

case 文の中で利用できます。

case <expression>
in <pattern1>
...
in <pattern2>
...
in <pattern3>
...
else
...
end

in 節と when 節は1つの case 式の中に混...
...には、例外が発生する

puts "Connect with user '#{user}'"
# Connect with user 'admin'" と出力
//}

『<expression> in <pattern>』 は 『<expression>; in <pattern>; true; else false; end』 と等価です。
パターンにマッチするかどうかだけを知りたいときに使え...
...tern> => variable』 または 『variable』 (「As パターン」, 「Variable パターン」)

Array/Hash パターンの中に 『<subpattern>』 と書かれている場所では任意のパターンをネストさせることができます。

Array パターン は配列か deconstruct...
...on>
in <pattern1>
...
in <pattern2>
...
in <pattern3>
...
else
...
end

in 節と when 節は1つの case 式の中に混ぜて書くことはできません。

『=>』 演算子と in 演算子で、単体の式で使用することも可能です。

<expression> => <...
...pattern>

<expression> in <pattern>

case/in 式は 「網羅的」 です。もし case 式の値がどの節にもマッチせず else 節がない場合、例外 NoMatchingPatternError が発生します。

そのため、条件付きのマッチや展開に case 式が使われることがあ...
...tern>』 と書かれている場所では任意のパターンをネストさせることができます。

Array パターン と Find パターン は配列か deconstruct メソッド(後述)を持つオブジェクトにマッチします。

Hash パターン はハッシュか deconstruct_ke...

MatchData#named_captures -> Hash (118.0)

名前付きキャプチャをHashで返します。

...= /(?<a>.)(?<b>.)/.match("01")
m.named_captures # => {"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 (118.0)

名前付きキャプチャをHashで返します。

...= /(?<a>.)(?<b>.)/.match("01")
m.named_captures # => {"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"}

m...
...= /(?<a>.)(?<a>.)/.match("01")
m.named_captures(symbolize_names: true) #=> {:a => "1"}
//}

@see MatchData#captures, MatchData#deconstruct_keys...

MatchData#captures -> [String] (101.0)

$1, $2, ... を格納した配列を返します。

...ば対応する要素は 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:spec/pattern_matching#matching_non_primitive_objec...