ライブラリ
- ビルトイン (86)
- csv (3)
- date (4)
-
json
/ add / ostruct (12) -
json
/ add / struct (12) - ostruct (60)
クラス
キーワード
-
deconstruct
_ keys (20) - dig (40)
- eql? (12)
- modifiable (12)
-
to
_ a (12) -
to
_ h (33) -
to
_ json (24)
検索結果
先頭5件
-
Struct
# hash -> Integer (27138.0) -
self が保持するメンバのハッシュ値を元にして算出した整数を返します。 self が保持するメンバの値が変化すればこのメソッドが返す値も変化します。
...も変化します。
//emlist[例][ruby]{
Dog = Struct.new(:name, :age)
dog = Dog.new("fred", 5)
p dog.hash #=> 7917421
dog.name = "john"
p dog.hash #=> -38913223
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対し......て呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。... -
OpenStruct
# hash -> Integer (21102.0) -
self のハッシュ値を返します。
self のハッシュ値を返します。 -
Struct
# deconstruct _ keys(array _ of _ names) -> Hash (15242.0) -
self のメンバの名前と値の組を Hash で返します。
...self のメンバの名前と値の組を Hash で返します。
@param array_of_names 返り値に含めるメンバの名前の配列を指定します。nil の場合は全てのメンバを意味します。
//emlist[例][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_keys(nil)
h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, A......nytown NC", :zip=>12345}
//}
[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。
@see d:spec/pattern_matching#matching_non... -
Struct
# to _ h -> Hash (9149.0) -
self のメンバ名(Symbol)と値の組を Hash にして返します。
...self のメンバ名(Symbol)と値の組を Hash にして返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_h
# => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
//}
ブロックを......omer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_h {|member, value|
[member, value*2]
} # => {:name=>"Joe SmithJoe Smith", :address=>"123 Maple, Anytown NC123 Maple, Anytown NC", :zip=>24690}
//}
[注意] 本メソッドの記述は Struct の......下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。... -
Struct
# to _ h {|member , value| block } -> Hash (9149.0) -
self のメンバ名(Symbol)と値の組を Hash にして返します。
...self のメンバ名(Symbol)と値の組を Hash にして返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_h
# => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
//}
ブロックを......omer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_h {|member, value|
[member, value*2]
} # => {:name=>"Joe SmithJoe Smith", :address=>"123 Maple, Anytown NC123 Maple, Anytown NC", :zip=>24690}
//}
[注意] 本メソッドの記述は Struct の......下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。... -
Struct
# to _ h -> Hash (9142.0) -
self のメンバ名(Symbol)と値の組を Hash にして返します。
...組を Hash にして返します。
//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345).to_h
# => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
//}
[注意] 本メソッドの記述は Struct の下位......クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。... -
Struct
# to _ json(*args) -> String (9025.0) -
自身を JSON 形式の文字列に変換して返します。
...てから JSON::Generator::GeneratorMethods::Hash#to_json を呼び出しています。
@param args 引数はそのまま JSON::Generator::GeneratorMethods::Hash#to_json に渡されます。
//emlist[例][ruby]{
require "json/add/core"
Person = Struct.new(:name, :age)
Person.new("tanaka", 29).t......o_json # => "{\"json_class\":\"Person\",\"v\":[\"tanaka\",29]}"
//}
@see JSON::Generator::GeneratorMethods::Hash#to_json... -
Struct
# dig(key , . . . ) -> object | nil (9013.0) -
self 以下のネストしたオブジェクトを dig メソッドで再帰的に参照して返し ます。途中のオブジェクトが nil であった場合は nil を返します。
...場合は nil を返します。
@param key キーを任意個指定します。
//emlist[例][ruby]{
klass = Struct.new(:a)
o = klass.new(klass.new({b: [1, 2, 3]}))
o.dig(:a, :a, :b, 0) # => 1
o.dig(:b, 0) # => nil
//}
@see Array#dig, Hash#dig, OpenStruct#dig... -
Hash
# dig(key , . . . ) -> object | nil (9007.0) -
self 以下のネストしたオブジェクトを dig メソッドで再帰的に参照して返し ます。途中のオブジェクトが nil であった場合は nil を返します。
...します。
@param key キーを任意個指定します。
//emlist[例][ruby]{
h = { foo: {bar: {baz: 1}}}
h.dig(:foo, :bar, :baz) # => 1
h.dig(:foo, :zot, :xyz) # => nil
g = { foo: [10, 11, 12] }
g.dig(:foo, 1) # => 11
//}
@see Array#dig, Struct#dig, OpenStruct#dig... -
CSV
:: Row # deconstruct _ keys(keys) -> Hash (6224.0) -
パターンマッチに使用するヘッダの名前と値の Hash を返します。
...パターンマッチに使用するヘッダの名前と値の Hash を返します。
このメソッドはヘッダ名の型をシンボルに変換しないため、ヘッダ名が文字列かつ Hash パターン でパターンマッチしたい場合はキーをシンボルに変換する... -
MatchData
# deconstruct _ keys(array _ of _ names) -> Hash (6224.0) -
引数で指定された名前の名前付きキャプチャを Hash で返します。
...引数で指定された名前の名前付きキャプチャを Hash で返します。
Hash のキーは名前付きキャプチャの名前のシンボル、値はキーの名前に対応した名前付きグループのうち最後にマッチした文字列です。
@param array_of_names 名......//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... -
Data
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6218.0) -
self のメンバの名前と値の組を Hash で返します。
...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" が表示され... -
Date
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6218.0) -
パターンマッチに使用する名前と値の Hash を返します。
...パターンマッチに使用する名前と値の Hash を返します。
キーに利用できる名前は以下の通りです。
* :year
* :month
* :day
* :yday
* :wday
@param array_of_names_or_nil パターンマッチに使用する名前の配列を指定します。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: ...2022
puts "... -
DateTime
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6218.0) -
パターンマッチに使用する名前と値の Hash を返します。
...パターンマッチに使用する名前と値の Hash を返します。
キーに利用できる名前は以下の通りです。
* :year
* :month
* :day
* :yday
* :wday
* :hour
* :min
* :sec
* :sec_fraction
* :zone
@param array_of_names_or_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: ...2022
puts "too old"
in month: ..9... -
Time
# deconstruct _ keys(array _ of _ names _ or _ nil) -> Hash (6218.0) -
パターンマッチに使用する名前と値の Hash を返します。
...パターンマッチに使用する名前と値の Hash を返します。
キーに利用できる名前は以下の通りです。
* :year
* :month
* :day
* :yday
* :wday
* :hour
* :min
* :sec
* :subsec
* :dst
* :zone
@param array_of_names_or_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" が出力される
case t
in year: ...2022... -
OpenStruct
# modifiable -> Hash (3102.0) -
このメソッドは内部的に使用されます。
このメソッドは内部的に使用されます。
自身が Object#freeze されている場合にこのメソッドを呼び出すと例外が発生します。
@raise TypeError 自身が Object#freeze されている場合に発生します。