るりまサーチ

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

別のキーワード

  1. struct new
  2. mkmf have_struct_member
  3. struct each_pair
  4. struct members
  5. kernel have_struct_member

ライブラリ

クラス

検索結果

Struct#deconstruct_keys(array_of_names) -> Hash (33244.0)

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

...ruby]{
Customer = Struct.new(:name, :address, :zip)
joe = Customer.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...
...:address=>"123 Maple, Anytown NC", :zip=>12345}
//}

[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。

@see d:spec/patter...

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

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

...の名前と値の組を 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...

MatchData#deconstruct_keys(array_of_names) -> Hash (24226.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...

Date#deconstruct_keys(array_of_names_or_nil) -> Hash (24214.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" が出力され...

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

絞り込み条件を変える

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

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

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

//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" が出力...

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

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

...に使用するヘッダの名前の配列を指定します。nil の場合は全てをパターンマッチに使用します。

//emlist[例][ruby]{
require "csv"

row = CSV::Row.new([:header1, :header2, :header3], [1, 2, 3])
case row
in { header1: 2.., header2: 2.., header3: 2.. }
puts "all 2...