るりまサーチ

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

別のキーワード

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

クラス

モジュール

キーワード

検索結果

<< 1 2 3 ... > >>

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

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

...Hash.new は {} と同じです。

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

//emlist[][ruby]{
h = Hash.new([])
h[0] << 0
h[1] << 1
p h....
...[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]...
...+= [0] #破壊的でないメソッドはOK
h[1] << 1
# エラー: can't modify frozen Array (RuntimeError)
//}...
...+= [0] #破壊的でないメソッドはOK
h[1] << 1
# エラー: can't modify frozen Array (FrozenError)
//}...

Enumerator::Yielder#<<(object) -> () (26154.0)

Enumerator.new で使うメソッドです。

...merator.new で使うメソッドです。

生成された Enumerator オブジェクトの each メソッドを呼ぶと
Enumerator::Yielder オブジェクトが渡されたブロックが実行され、
ブロック内の << が呼ばれるたびに each に渡されたブロックが
<<
に渡...
...された値とともに繰り返されます。

//emlist[例][ruby]{
enum = Enumerator.new do |y|
y << 1
y << 2
y << 3
end

enum.each do |v|
p v
end
# => 1
# 2
# 3
//}...

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

空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になりま す。設定したデフォルト値は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] #=> "foobar"

p h[2] #=> "foobar"
p h...
...ロックを評価するので、全て別のオブジェクトになります。
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] #=> "foobar"

p h[2] #=>...
...#=> {1=>"foobar", 2=>"foo"}

# 値が設定されていないときに(fetchのように)例外をあげるようにもできる
h = Hash.new {|hash, key|
raise(IndexError, "hash[#{key}] has no value")
}
h[1]
# エラー hash[1] has no value (IndexError)
//}...

Enumerator.new(size=nil) {|y| ... } -> Enumerator (26141.0)

Enumerator オブジェクトを生成して返します。与えられたブロックは Enumerator::Yielder オブジェクトを 引数として実行されます。

...の生成時に指定されたブロックを
実行し、Yielder オブジェクトに対して << メソッドが呼ばれるたびに、
each に渡されたブロックが繰り返されます。

new
に渡されたブロックが終了した時点で each の繰り返しが終わります。...
...行時に参照されます。

//emlist[例][ruby]{
enum = Enumerator.new{|y|
(1..10).each{|i|
y << i if i % 5 == 0
}
}
enum.each{|i| p i }

#=> 5
# 10


fib = Enumerator.new { |y|
a = b = 1
loop {
y << a
a, b = b, a + b
}
}

p fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21...

Enumerator::Lazy.new(obj, size=nil) {|yielder, *values| ... } -> Enumerator::Lazy (26121.0)

Lazy Enumerator を作成します。Enumerator::Lazy#force メソッドなどに よって列挙が実行されたとき、objのeachメソッドが実行され、値が一つずつ ブロックに渡されます。ブロックは、yielder を使って最終的に yield される値を 指定できます。

...le
def filter_map(&block)
map(&block).compact
end
end

class Enumerator::Lazy
def filter_map
Lazy.new(self) do |yielder, *values|
result = yield *values
yielder << result if result
end
end
end

1.step.lazy.filter_map{|i| i*i if i.even?}.first(5)
# => [4, 16, 36, 6...
...4, 100]
//}

@raise ArgumentError 引数を指定しなかった場合、ブロックを指定しなかった場合に発生します。

@see Enumerator.new...

絞り込み条件を変える

Enumerator.new(obj, method = :each, *args) -> Enumerator (26116.0)

オブジェクト obj について、 each の代わりに method という 名前のメソッドを使って繰り返すオブジェクトを生成して返します。 args を指定すると、 method の呼び出し時に渡されます。

...method イテレータメソッドの名前を表すシンボルまたは文字列
@param args イテレータメソッドの呼び出しに渡す任意個の引数

//emlist[例][ruby]{
str = "xyz"

enum = Enumerator.new(str, :each_byte)
p enum.map {|b| '%02x' % b } # => ["78", "79", "7a"]
//}...

Array#pack(template, buffer: String.new) -> String (8133.0)

配列の内容を template で指定された文字列にしたがって、 バイナリとしてパックした文字列を返します。

...sgn * Inf # ±∞ (positive/negative infinity)
else
NaN # 非数 (not a number)
end
else
fra += 1 << 23 # ゲタ
sgn * fra * 2**(exp-127-23) # 正規化数 (normalized number)
end
//}

: G

ビッグエンディアンの倍精...
...sgn * Inf # ±∞ (positive/negative infinity)
else
NaN # 非数 (not a number)
end
else
fra += 1 << 52 # ゲタ
sgn * fra * 2**(exp-1023-52) # 正規化数 (normalized number)
end
//}

: p

ヌル終端の文字列へのポイ...
...82, 117, 98, 121].pack("c*") # => "Ruby"

s = ""
[82, 117, 98, 121].each {|c| s << c}
s # => "Ruby"

[82, 117, 98, 121].collect {|c| sprintf "%c", c}.join # => "Ruby"

[82, 117, 98, 121].inject("") {|s, c| s << c} # => "Ruby"
//}

: 文字列を数値(文字コード)の配列に変換す...

Object#singleton_methods(inherited_too = true) -> [Symbol] (8038.0)

そのオブジェクトに対して定義されている特異メソッド名 (public あるいは protected メソッド) の一覧を返します。

...定します。

//emlist[例1][ruby]{
Parent = Class.new

class <<Parent
private; def private_class_parent() end
protected; def protected_class_parent() end
public; def public_class_parent() end
end

Foo = Class.new(Parent)

class <<Foo
private; def private_class_foo() end
protecte...
...d
end

module Bar
private; def private_bar() end
protected; def protected_bar() end
public; def public_bar() end
end

obj = Foo.new
class <<obj
include Bar
private; def private_self() end
protected; def protected_self() end
public; def public_self() end
end

# あ...

Array#pack(template) -> String (8033.0)

配列の内容を template で指定された文字列にしたがって、 バイナリとしてパックした文字列を返します。

...sgn * Inf # ±∞ (positive/negative infinity)
else
NaN # 非数 (not a number)
end
else
fra += 1 << 23 # ゲタ
sgn * fra * 2**(exp-127-23) # 正規化数 (normalized number)
end
//}

: G

ビッグエンディアンの倍精...
...sgn * Inf # ±∞ (positive/negative infinity)
else
NaN # 非数 (not a number)
end
else
fra += 1 << 52 # ゲタ
sgn * fra * 2**(exp-1023-52) # 正規化数 (normalized number)
end
//}

: p

ヌル終端の文字列へのポイ...
...82, 117, 98, 121].pack("c*") # => "Ruby"

s = ""
[82, 117, 98, 121].each {|c| s << c}
s # => "Ruby"

[82, 117, 98, 121].collect {|c| sprintf "%c", c}.join # => "Ruby"

[82, 117, 98, 121].inject("") {|s, c| s << c} # => "Ruby"
//}

: 文字列を数値(文字コード)の配列に変換す...

Method#inspect -> String (8026.0)

self を読みやすい文字列として返します。

...

//emlist[例][ruby]{
module Foo
def foo
"foo"
end
end
class Bar
include Foo
def bar
end
end

p Bar.new.method(:foo) # => #<Method: Bar(Foo)#foo>
p Bar.new.method(:bar) # => #<Method: Bar#bar>
//}

klass1 と klass2 が同じ場合は以下の形式になります。...
...メソッドを定義しているオブジェクトになります。

//emlist[例][ruby]{
# オブジェクトの特異メソッド
obj = ""
class <<obj
def foo
end
end
p obj.method(:foo) # => #<Method: "".foo>

# クラスメソッド(クラスの特異メソッド)
class Foo
def Foo.f...
...# スーパークラスのクラスメソッド
class Bar < Foo
end
p Bar.method(:foo) # => #<Method: Bar.foo>

# 以下は(形式1)の出力になる
module Baz
def baz
end
end

class <<obj
include Baz
end
p obj.method(:baz) # => #<Method: Object(Baz)#baz>
//}

@see Object#inspect...
...st[例][ruby]{
module Foo
def foo
"foo"
end
end
class Bar
include Foo
def bar(a, b)
end
end

p Bar.new.method(:foo) # => #<Method: Bar(Foo)#foo() test.rb:2>
p Bar.new.method(:bar) # => #<Method: Bar#bar(a, b) test.rb:8>
//}

klass1 と klass2 が同じ場合は以下の...
...メソッドを定義しているオブジェクトになります。

//emlist[例][ruby]{
# オブジェクトの特異メソッド
obj = ""
class <<obj
def foo
end
end
p obj.method(:foo) # => #<Method: "".foo() foo.rb:4>

# クラスメソッド(クラスの特異メソッド)
class Foo...
...ラスメソッド
class Bar < Foo
end
p Bar.method(:foo) # => #<Method: Bar(Foo).foo() foo.rb:11>

# 以下は(形式1)の出力になる
module Baz
def baz
end
end

class <<obj
include Baz
end
p obj.method(:baz) # => #<Method: String(Baz)#baz() foo.rb:23>
//}

@see Object#inspect...

絞り込み条件を変える

<< 1 2 3 ... > >>