るりまサーチ

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

別のキーワード

  1. _builtin to_a
  2. matrix to_a
  3. to_a
  4. dbm to_a
  5. argf.class to_a

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 3 ... > >>

Enumerator::Yielder#yield(*object) -> () (24120.0)

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

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

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

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

enum.each do |x, y, z|
p [x, y, z]
end
# => [1, 2, 3]
//}...

Proc#yield(*arg) -> () (15202.0)

手続きオブジェクトを実行してその結果を返します。

...て異なります。
詳しくは Proc#lambda? を参照してください。

「===」は when の所に手続きを渡せるようにするためのものです。

//emlist[例][ruby]{
def sign(n)
case n
when lambda{|n| n > 0} then 1
when lambda{|n| n < 0} then -1
else 0
end
end

p si...
...ような syntactic sugar もあります。

//emlist[例][ruby]{
fib = lambda{|n|
case n
when 0 then 0
when 1 then 1
else
fib.(n - 2) + fib.(n - 1)
end
}
fib.(10) # => 55
//}



@param arg 手続きオブジェクトに与える引数を指定します。

@raise LocalJumpError Proc...

Exception#backtrace_locations -> [Thread::Backtrace::Location] (12407.0)

バックトレース情報を返します。Exception#backtraceに似ていますが、 Thread::Backtrace::Location の配列を返す点が異なります。

...on#backtraceに似ていますが、
Thread::Backtrace::Location の配列を返す点が異なります。

現状では Exception#set_backtrace によって戻り値が変化する事はあり
ません。

//emlist[例: test.rb][ruby]{
require "date"
def check_long_month(month)
return if Date.n...
...w(2000, month, -1).day == 31
raise "#{month} is not long month"
end

def get_exception
return begin
yield

rescue => e
e
end
end

e = get_exception { check_long_month(2) }
p e.backtrace_locations
# => ["test.rb:4:in `check_long_month'", "test.rb:15:in `block in <main>'", "test.rb:9:in...
...`get_exception'", "test.rb:15:in `<main>'"]
//}

@see Exception#backtrace...

Thread#thread_variable_get(key) -> object | nil (9107.0)

引数 key で指定した名前のスレッドローカル変数を返します。

...ad#[] でセットしたローカル変数(Fiber ローカル変数)と
異なり、Fiber を切り替えても同じ変数を返す事に注意してください。

例:

Thread.new {
Thread.current.thread_variable_set("foo", "bar") # スレッドローカル
Thread.current["foo"] = "ba...
...iber.new {
Fiber.yield [
Thread.current.thread_variable_get("foo"), # スレッドローカル
Thread.current["foo"], # Fiber ローカル
]
}.resume
}.join.value # => ['bar', nil]

この例の "bar" は Thread#thread_variable_get により得...
...られ
た値で、nil はThread#[] により得られた値です。

@see Thread#thread_variable_set, Thread#[]

@see https://magazine.rubyist.net/articles/0041/0041-200Special-note.html...

Object#yield_self -> Enumerator (6239.0)

self を引数としてブロックを評価し、ブロックの結果を返します。

...f を引数としてブロックを評価し、ブロックの結果を返します。

//emlist[例][ruby]{
"my string".yield_self {|s| s.upcase } # => "MY STRING"
3.next.yield_self {|x| x**x }.to_s # => "256"
//}

値をメソッドチェインのパイプラインに次々と渡すのは...
...プライン][ruby]{
require 'open-uri'
require 'json'

construct_url(arguments).
yield
_self {|url| URI(url).read }.
yield
_self {|response| JSON.parse(response) }
//}

ブロックなしで呼び出されたときは Enumerator を返します。
例えば条件によって値を捨てるのに...
...使えます。

//emlist[][ruby]{
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
//}

@see Object#tap...

絞り込み条件を変える

Enumerable#max_by -> Enumerator (6179.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...す。

Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a
= %w(albatross dog horse)
a
.max_by...
...# => #<Enumerator: ["albatross", "dog", "horse"]:max_by>
a
.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a
= %w[albatross dog horse]
a
.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a
.max_by(2) {|x| x.length } # => ["albatross", "horse"...
...num.max_by(n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampli...

Enumerable#max_by {|item| ... } -> object | nil (6179.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...す。

Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a
= %w(albatross dog horse)
a
.max_by...
...# => #<Enumerator: ["albatross", "dog", "horse"]:max_by>
a
.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a
= %w[albatross dog horse]
a
.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a
.max_by(2) {|x| x.length } # => ["albatross", "horse"...
...num.max_by(n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampli...

Enumerable#max_by(n) -> Enumerator (6179.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...す。

Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a
= %w(albatross dog horse)
a
.max_by...
...# => #<Enumerator: ["albatross", "dog", "horse"]:max_by>
a
.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a
= %w[albatross dog horse]
a
.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a
.max_by(2) {|x| x.length } # => ["albatross", "horse"...
...num.max_by(n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampli...

Enumerable#max_by(n) {|item| ... } -> Array (6179.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...す。

Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a
= %w(albatross dog horse)
a
.max_by...
...# => #<Enumerator: ["albatross", "dog", "horse"]:max_by>
a
.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a
= %w[albatross dog horse]
a
.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a
.max_by(2) {|x| x.length } # => ["albatross", "horse"...
...num.max_by(n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampli...

Enumerator#next_values -> Array (6179.0)

「次」のオブジェクトを配列で返します。

...次」のオブジェクトを配列で返します。

Enumerator#next とほぼ同様の挙動をします。終端まで到達した場合は
StopIteration 例外を発生させます。

このメソッドは、
yield


yield
nil
を区別するために使えます。

next メソッド...
...each_line のようにおおもとの列挙メカニズムが副作用を
伴っている場合には影響があり得ます。

//emlist[例: next と next_values の違いを][ruby]{
o = Object.new
def o.each
yield

yield
1
yield
1, 2
yield
nil
yield
[1, 2]
end
e = o.to_enum
p e.next_val...
...next_values
p e.next_values
p e.next_values
p e.next_values
e = o.to_enum
p e.next
p e.next
p e.next
p e.next
p e.next

## yield args next_values next
# yield [] nil
# yield 1 [1] 1
# yield 1, 2 [1, 2] [1, 2]
# yield nil...

絞り込み条件を変える

<< 1 2 3 ... > >>