るりまサーチ

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

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 3 ... > >>

Hash.new {|hash, key| ... } -> Hash (35414.0)

空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になりま す。設定したデフォルト値はHash#default_procで参照できます。

...空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になりま
す。設定したデフォルト値はHash#default_procで参照できます。

値が設定されていないハッシュ要素を参照するとその都度ブロックを
実行し、そ...
...ルト値は全部同一のオブジェクトなので、
# 破壊的変更によって他のキーに対応する値も変更されます。
h = Hash.new("foo")

p h[1] #=> "foo"
p h[1].object_id #=> 6127170
p h[1] << "bar" #=> "foobar"
p h[1] #=> "f...
...まだ無いキーが呼び出される度に
# ブロックを評価するので、全て別のオブジェクトになります。
h = Hash.new {|hash, key| hash[key] = "foo"}

p h[1] #=> "foo"
p h[1].object_id #=> 6126900
p h[1] << "bar" #=> "foobar"
p h[1]...

Hash.new(ifnone = nil) -> Hash (35284.0)

空の新しいハッシュを生成します。ifnone はキーに対 応する値が存在しない時のデフォルト値です。設定したデフォルト値はHash#defaultで参照できます。

...one はキーに対
応する値が存在しない時のデフォルト値です。設定したデフォルト値はHash#defaultで参照できます。

ifnoneを省略した Hash.new は {} と同じです。

デフォルト値として、毎回同一のオブジェクトifnoneを返します。...
...それにより、一箇所のデフォルト値の変更が他の値のデフォルト値にも影響します。

//emlist[][ruby]{
h = Hash.new([])
h[0] << 0
h[1] << 1
p h.default #=> [0, 1]
//}

これを避けるには、破壊的でないメソッドで再代入する必要が有ります。...
...][ruby]{
h = Hash.new([])

p h[1] #=> []
p h[1].object_id #=> 6127150
p h[1] << "bar" #=> ["bar"]
p h[1] #=> ["bar"]

p h[2] #=> ["bar"]
p h[2].object_id #=> 6127150

p h #=> {}


h = Hash.new([].freeze)
h[0...

Data#hash -> Integer (26143.0)

自身のハッシュ値を整数で返します。 Data#eql? で比較して等しいオブジェクトは同じハッシュ値を返します。

...す。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
dog1 = Dog.new("Fred", 5)
p dog1.hash # => -3931425561194935428
dog2 = Dog.new("Fred", 5)
p dog2.hash # => -3931425561194935428
dog3 = Dog.new("Fred", 6)
p dog3.hash # => -4469132459285820530
//}

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

@see Object#hash...

Struct#hash -> Integer (26131.0)

self が保持するメンバのハッシュ値を元にして算出した整数を返します。 self が保持するメンバの値が変化すればこのメソッドが返す値も変化します。

....new(:name, :age)
dog = Dog.new("fred", 5)
p dog.hash #=> 7917421
dog.name = "john"
p dog.hash #=> -38913223
//}

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

Struct.new(*args, keyword_init: nil) -> Class (23237.0)

Struct クラスに新しいサブクラスを作って、それを返します。

...では構造体のメンバに対するアクセスメソッドが定義されています。

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "name:%s age:%d", fred.name, fred.age
#=> "name:fred age:6" を出力します
//}

実装の都合...
...emlist[例][ruby]{
Point = Struct.new(:x, :y, keyword_init: true) # => Point(keyword_init: true)
Point.new(x: 1, y: 2) # => #<struct Point x=1, y=2>
Point.new(x: 1) # => #<struct Point x=1, y=nil>
Point.new(y: 2) # => #<struct Point x=nil, y=2>
Point.new(z: 3) # ArgumentError (unkn...
...{
Point = Struct.new(:x, :y)
Point.new(x: 1, y: 2) # => #<struct Point x={:x=>1, :y=>2}, y=nil>
# warning: Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. Please use a Hash literal like .new({k: v}) instead of .new(k: v).

# keywor...

絞り込み条件を変える

Struct.new(*args, keyword_init: nil) {|subclass| block } -> Class (23237.0)

Struct クラスに新しいサブクラスを作って、それを返します。

...では構造体のメンバに対するアクセスメソッドが定義されています。

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "name:%s age:%d", fred.name, fred.age
#=> "name:fred age:6" を出力します
//}

実装の都合...
...emlist[例][ruby]{
Point = Struct.new(:x, :y, keyword_init: true) # => Point(keyword_init: true)
Point.new(x: 1, y: 2) # => #<struct Point x=1, y=2>
Point.new(x: 1) # => #<struct Point x=1, y=nil>
Point.new(y: 2) # => #<struct Point x=nil, y=2>
Point.new(z: 3) # ArgumentError (unkn...
...{
Point = Struct.new(:x, :y)
Point.new(x: 1, y: 2) # => #<struct Point x={:x=>1, :y=>2}, y=nil>
# warning: Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. Please use a Hash literal like .new({k: v}) instead of .new(k: v).

# keywor...

Struct.new(*args) -> Struct (23137.0)

(このメソッドは Struct の下位クラスにのみ定義されています) 構造体オブジェクトを生成して返します。

...なければ nil です。

@return 構造体クラスのインスタンス。

@raise ArgumentError 構造体のメンバの数よりも多くの引数を指定した場合に発生します。

//emlist[例][ruby]{
Foo = Struct.new(:foo, :bar)
foo = Foo.new(1)
p foo.values # => [1, nil]
//}...

RubyVM::InstructionSequence.new(source, file = nil, path = nil, line = 1, options = nil) -> RubyVM::InstructionSequence (23107.0)

引数 source で指定した Ruby のソースコードを元にコンパイル済みの RubyVM::InstructionSequence オブジェクトを作成して返します。

...

@param line 引数 source の 1 行目の行番号を指定します。

@param options コンパイル時のオプションを true、false、Hash オブ
ジェクトのいずれかで指定します。詳細は
RubyVM::InstructionSequence.compile_option= を参...

Hash#to_h -> self | Hash (17212.0)

self を返します。Hash クラスのサブクラスから呼び出した場合は self を Hash オブジェクトに変換します。

...す。Hash クラスのサブクラスから呼び出した場合は
self を Hash オブジェクトに変換します。

//emlist[例][ruby]{
hash
= {}
p hash.to_h # => {}
p hash.to_h == hash # => true

class MyHash < Hash;end
my_hash = MyHash.new
p my_hash.to_h # => {}
p my_hash.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}

ブロックを指定すると各ペアでブロックを呼び出し、
その結果をペアとして使います。
//emlist[ブロック付きの例][ruby]{
hash
= { "a" => 97, "b" => 98 }
hash
.to_h {|key, value| [key.upcase, value-32] } #...

Hash#to_h {|key, value| block } -> Hash (17212.0)

self を返します。Hash クラスのサブクラスから呼び出した場合は self を Hash オブジェクトに変換します。

...す。Hash クラスのサブクラスから呼び出した場合は
self を Hash オブジェクトに変換します。

//emlist[例][ruby]{
hash
= {}
p hash.to_h # => {}
p hash.to_h == hash # => true

class MyHash < Hash;end
my_hash = MyHash.new
p my_hash.to_h # => {}
p my_hash.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}

ブロックを指定すると各ペアでブロックを呼び出し、
その結果をペアとして使います。
//emlist[ブロック付きの例][ruby]{
hash
= { "a" => 97, "b" => 98 }
hash
.to_h {|key, value| [key.upcase, value-32] } #...

絞り込み条件を変える

Hash#to_h -> self | Hash (17199.0)

self を返します。Hash クラスのサブクラスから呼び出した場合は self を Hash オブジェクトに変換します。

...す。Hash クラスのサブクラスから呼び出した場合は
self を Hash オブジェクトに変換します。

//emlist[例][ruby]{
hash
= {}
p hash.to_h # => {}
p hash.to_h == hash # => true

class MyHash < Hash;end
my_hash = MyHash.new
p my_hash.to_h # => {}
p my_hash.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}...

Hash.[](*key_and_value) -> Hash (17153.0)

新しいハッシュを生成します。 引数は必ず偶数個指定しなければなりません。奇数番目がキー、偶数番目が値になります。

...値になります。

このメソッドでは生成するハッシュにデフォルト値を指定することはできません。
Hash
.newを使うか、Hash#default=で後から指定してください。

@param key_and_value 生成するハッシュのキーと値の組です。必ず偶数...
...][ruby]{
ary = [1,"a", 2,"b", 3,["c"]]
p Hash[*ary] # => {1=>"a", 2=>"b", 3=>["c"]}
//}

(2) キーと値のペアの配列からハッシュへ

//emlist[][ruby]{
alist = [[1,"a"], [2,"b"], [3,["c"]]]
p alist.flatten(1) # => [1, "a", 2, "b", 3, ["c"]]
p Hash[*alist.flatten(1)] # => {1=>"a", 2=>"b...
...ls].transpose
p alist # => [[1, "a"], [2, "b"], [3, ["c"]]]
p Hash[alist] # => {1=>"a", 2=>"b", 3=>["c"]}
//}

(4) キーや値が配列の場合

//emlist[][ruby]{
alist = [[1,["a"]], [2,["b"]], [3,["c"]], [[4,5], ["a", "b"]]]
hash
= Hash[alist] # => {1=>["a"], 2=>["b"], 3=>["c"], [4, 5]=>["a", "b...

Hash#merge(*others) -> Hash (17138.0)

selfとothersのハッシュの内容を順番にマージ(統合)した結果を返します。 デフォルト値はselfの設定のままです。

...値を使います。

othersがハッシュではない場合、othersのメソッドto_hashを使って暗黙の変換を試みます。

@param others マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マージした結...
...1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
h1.merge(h2) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>46, "c"=>300}
h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
h1...
...to_hash
{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#r...

Hash#merge(*others) {|key, self_val, other_val| ... } -> Hash (17138.0)

selfとothersのハッシュの内容を順番にマージ(統合)した結果を返します。 デフォルト値はselfの設定のままです。

...値を使います。

othersがハッシュではない場合、othersのメソッドto_hashを使って暗黙の変換を試みます。

@param others マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マージした結...
...1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
h1.merge(h2) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>46, "c"=>300}
h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
h1...
...to_hash
{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#r...

Hash#merge(other) -> Hash (17138.0)

selfとotherのハッシュの内容をマージ(統合)した結果を返します。デフォルト値はselfの設定のままです。

...の値を使います。

otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

@param other マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マージした結...
..."b" => 254, "c" => 300 }
h1.merge() # => {"a"=>100, "b"=>200}
h1.merge(h2) # => {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
# => {"a"=>100, "b"=>54, "c"=>300}
h1 # => {"a"=>100, "b"=>200}
//}

//emlist[][ruby]{
foo = {1 => 'a'...
...to_hash
{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#r...

絞り込み条件を変える

Hash#merge(other) {|key, self_val, other_val| ... } -> Hash (17138.0)

selfとotherのハッシュの内容をマージ(統合)した結果を返します。デフォルト値はselfの設定のままです。

...の値を使います。

otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

@param other マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マージした結...
..."b" => 254, "c" => 300 }
h1.merge() # => {"a"=>100, "b"=>200}
h1.merge(h2) # => {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
# => {"a"=>100, "b"=>54, "c"=>300}
h1 # => {"a"=>100, "b"=>200}
//}

//emlist[][ruby]{
foo = {1 => 'a'...
...to_hash
{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#r...

Hash.[](other) -> Hash (17128.0)

新しいハッシュを生成します。 引数otherと同一のキーと値を持つ新たなハッシュを生成して返します。

...のキーと値を持つ新たなハッシュを生成して返します。

引数otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

デフォルト値はコピーしません。生成されたハッシュのデフォルト値は nil...
...します。

@param other 生成元となるハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。

//emlist[][ruby]{
h = {1 => "value"}
h.default = "none"

g = Hash[h]
p g #=> {1=>"value"}

p h[:no] #=> "none"
p g[:no] #=> nil

h[:add] = "some"
p h #...
<< 1 2 3 ... > >>