るりまサーチ

最速Rubyリファレンスマニュアル検索!
22件ヒット [1-22件を表示] (0.118秒)

別のキーワード

  1. _builtin deconstruct_keys
  2. _builtin deconstruct
  3. struct deconstruct_keys
  4. struct deconstruct
  5. date deconstruct_keys

ライブラリ

クラス

検索結果

Data#deconstruct_keys(array_of_names_or_nil) -> Hash (18120.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 (18120.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 (18114.0)

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

...er.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 Ma...

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

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

...l の場合は全てをパターンマッチに使用します。

//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: ...2022...

DateTime#deconstruct_keys(array_of_names_or_nil) -> Hash (18108.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 (18108.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 (18102.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...

MatchData#named_captures -> Hash (7.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 (7.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...