るりまサーチ

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

別のキーワード

  1. _builtin puts
  2. stringio puts
  3. csv puts
  4. buffering puts
  5. outputmethod puts

ライブラリ

クラス

キーワード

検索結果

Data#deconstruct -> [object] (18131.0)

self のメンバの値を配列で返します。

...)
distance.deconstruct # => [10, "km"]
//}

このメソッドは以下のようにパターンマッチで利用されます。

//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)
distance = Measure.new(10, 'km')

case distance
in n, 'km' # 裏側で #deconstruct を呼ぶ
puts
"It is #{n...
...} kilometers away"
else
puts
"Don't know how to handle it"
end
# "It is 10 kilometers away" が表示される

# 以下のようにも書ける
case distance
in Measure(n, 'km')
puts
"It is #{n} kilometers away"
# ...
end
//}

[注意] 本メソッドの記述は Data のサブクラスのイ...

CSV::Row#deconstruct -> [object] (18113.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 2, and rest columns are 2 o...

Data#deconstruct_keys(array_of_names_or_nil) -> Hash (6137.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}
//}

このメソッドは以下のようにパターンマッチで利用...
..., 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" が表示される

# 以下のようにも書ける
case distance
in Measure(amount:, unit: 'km')
puts
"It is #{amount} kilomete...

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

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

...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
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" が出力される

# クラスのチェックと組み合わせて利用することもできます
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 (6137.0)

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

...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
puts
"quarter 1-3"
in wday: 1..5, month:
puts
"working day in month #{month}"
end
#=> "work...
...ing 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_primitive_objects...

絞り込み条件を変える

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

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

...0)

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
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" が出力される

# クラスのチェックと組み合わせて利用することもできます
if t in Time(wday: 3, day: ..7)
puts
"first Wednesday of the month"
end
//}

@see d:spec/pattern_matching#matching_non_primitive_objects...

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

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

...SV::Row.new([:header1, :header2, :header3], [1, 2, 3])
case row
in { header1: 2.., header2: 2.., header3: 2.. }
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...

パターンマッチ (180.0)

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

...r:} # ネストしてハッシュにマッチして、その値を変数userに代入する
puts
"Connect with user '#{user}'"
in connection: {username: }
puts
"Connect with user '#{username}'"
else
puts
"Unrecognized structure of config"
end
# "Connect with user 'admin'" と出力
//}

一方...
...n', password: 'abc123'}}

config in {db: {user:}} # もし config の構造が期待したものでなかった場合には、例外が発生する

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

『<expression> in <pattern>』 は 『<expression>; in <pattern>; true;...
...ンをネストさせることができます。

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

Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash パ...
...n', password: 'abc123'}}

config => {db: {user:}} # もし config の構造が期待したものでなかった場合には、例外が発生する

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

『<expression> in <pattern>』 は 『<expression>; in <pattern>; true;...
...ることができます。

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

Hash パターン はハッシュか deconstruct_keys メソッド(後述)を持つオブジェクトにマッチします。Hash...