クラス
- Enumerator (84)
-
Enumerator
:: Lazy (48) -
Enumerator
:: Yielder (30) - Exception (24)
- Fiber (54)
- Object (98)
- Pathname (39)
- Proc (48)
- StopIteration (12)
- Thread (24)
モジュール
- Enumerable (100)
- Kernel (12)
キーワード
- << (12)
- == (12)
- === (12)
- [] (24)
- alive? (12)
-
backtrace
_ locations (12) - call (12)
- compact (4)
-
create
_ makefile (12) - each (48)
-
each
_ entry (39) -
enum
_ for (48) - feed (12)
- find (24)
-
max
_ by (48) -
next
_ values (12) -
peek
_ values (12) - raise (18)
- result (12)
- resume (12)
-
sort
_ by (24) - tap (8)
- then (14)
-
thread
_ variable _ get (12) -
to
_ enum (48) -
to
_ proc (18) - transfer (12)
-
yield
_ self (16)
検索結果
先頭5件
-
Enumerable
# max _ by(n) -> Enumerator (7.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...ng with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end
end
e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p... -
Enumerable
# max _ by(n) {|item| . . . } -> Array (7.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...ng with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end
end
e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p... -
Enumerable
# sort _ by -> Enumerator (7.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...成して返します。
つまり、以下とほぼ同じ動作をします。
//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}
Enumerable#sort と比較して sort_by が優れている点... -
Enumerable
# sort _ by {|item| . . . } -> [object] (7.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...成して返します。
つまり、以下とほぼ同じ動作をします。
//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}
Enumerable#sort と比較して sort_by が優れている点... -
Enumerator
:: Lazy # enum _ for(method = :each , *args) -> Enumerator :: Lazy (7.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_given?
each do |*val|
n.times { yield *val }
end
else
to_enum(:repeat, n)
end
end
end
r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]
r = 1..... -
Enumerator
:: Lazy # enum _ for(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (7.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_given?
each do |*val|
n.times { yield *val }
end
else
to_enum(:repeat, n)
end
end
end
r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]
r = 1..... -
Enumerator
:: Lazy # to _ enum(method = :each , *args) -> Enumerator :: Lazy (7.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_given?
each do |*val|
n.times { yield *val }
end
else
to_enum(:repeat, n)
end
end
end
r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]
r = 1..... -
Enumerator
:: Lazy # to _ enum(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (7.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_given?
each do |*val|
n.times { yield *val }
end
else
to_enum(:repeat, n)
end
end
end
r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]
r = 1..... -
Exception
# ==(other) -> bool (7.0) -
自身と指定された other のクラスが同じであり、 message と backtrace が == メソッドで比較して 等しい場合に true を返します。そうでない場合に false を返します。
...heck_long_month(month)
return if Date.new(2000, month, -1).day == 31
raise "#{month} is not long month"
end
def get_exception
return begin
yield
rescue => e
e
end
end
results = [2, 2, 4].map { |e | get_exception { check_long_month(e) } }
p results.map { |e| e.class }
# => [Runtim... -
Exception
# backtrace _ locations -> [Thread :: Backtrace :: Location] (7.0) -
バックトレース情報を返します。Exception#backtraceに似ていますが、 Thread::Backtrace::Location の配列を返す点が異なります。
...heck_long_month(month)
return if Date.new(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....