るりまサーチ

最速Rubyリファレンスマニュアル検索!
282件ヒット [101-200件を表示] (0.085秒)

別のキーワード

  1. rbconfig ruby
  2. fiddle ruby_free
  3. fiddle build_ruby_platform
  4. rake ruby
  5. rubygems/defaults ruby_engine

ライブラリ

クラス

モジュール

キーワード

検索結果

<< < 1 2 3 > >>

Object#to_enum(method = :each, *args) {|*args| ... } -> Enumerator (153.0)

Enumerator.new(self, method, *args) を返します。

...

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

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

# protects an array from being modified
a = [1, 2, 3]
p(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumer...
...e
sz * n if sz
end
end
each
do |*val|
n.times { yield *val }
end
end
end

%i[hello world].repeat(2) { |w| puts w }
# => 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => #<Enumerator: 1..14:repeat(3)>
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42...

Enumerator::Lazy#enum_for(method = :each, *args) -> Enumerator::Lazy (135.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[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..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげで、repeat の返り値は
# もとが Enumerator のときは...

Enumerator::Lazy#enum_for(method = :each, *args) {|*args| block} -> Enumerator::Lazy (135.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[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..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげで、repeat の返り値は
# もとが Enumerator のときは...

Enumerator::Lazy#to_enum(method = :each, *args) -> Enumerator::Lazy (135.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[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..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげで、repeat の返り値は
# もとが Enumerator のときは...

Enumerator::Lazy#to_enum(method = :each, *args) {|*args| block} -> Enumerator::Lazy (135.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[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..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげで、repeat の返り値は
# もとが Enumerator のときは...

絞り込み条件を変える

Enumerable#inject(init = self.first) {|result, item| ... } -> object (133.0)

リストのたたみこみ演算を行います。

...ブジェクトを指定します。
実行結果に対して sym という名前のメソッドが呼ばれます。

//emlist[例][ruby]{
# 合計を計算する。
p [2, 3, 4, 5].inject {|result, item| result + item } #=> 14

# 自乗和を計算する。初期値をセット...
..., item| result + item**2 } #=> 54
//}

この式は以下のように書いても同じ結果が得られます。

//emlist[例][ruby]{
result = 0
[1, 2, 3, 4, 5].each {|v| result += v }
p result # => 15

p [1, 2, 3, 4, 5].inject(:+) #=> 15
p ["b", "c", "d"].inject("abbccddde"...

Enumerable#reduce(init = self.first) {|result, item| ... } -> object (133.0)

リストのたたみこみ演算を行います。

...ブジェクトを指定します。
実行結果に対して sym という名前のメソッドが呼ばれます。

//emlist[例][ruby]{
# 合計を計算する。
p [2, 3, 4, 5].inject {|result, item| result + item } #=> 14

# 自乗和を計算する。初期値をセット...
..., item| result + item**2 } #=> 54
//}

この式は以下のように書いても同じ結果が得られます。

//emlist[例][ruby]{
result = 0
[1, 2, 3, 4, 5].each {|v| result += v }
p result # => 15

p [1, 2, 3, 4, 5].inject(:+) #=> 15
p ["b", "c", "d"].inject("abbccddde"...

Enumerable#slice_before {|elt| bool } -> Enumerator (49.0)

パターンがマッチした要素、もしくはブロックが真を返した要素から 次にマッチする手前までを チャンク化(グループ化)したものを繰り返す Enumerator を 返します。

...配列として表現されます。

Enumerable#to_a や Enumerable#map のようなメソッドを使うこ
ともできます。

//emlist[例][ruby]{
# 偶数要素をチャンクの先頭と見なす
[0,2,4,1,2,4,5,3,1,4,2].slice_before(&:even?).to_a
# => [[0], [2], [4, 1], [2], [4, 5, 3, 1], [...
...トリーを順に取る
open("ChangeLog") {|f|
f.slice_before(/\A\S/).each {|e| pp e}
}

# 上と同じだが、パターンでなくブロックを使う
open("ChangeLog") {|f|
f.slice_before {|line| /\A\S/ === line }.each {|e| pp e}
}

# "svn proplist -R" の結果を分割する
# これは...
...IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) {|f|
f.lines.slice_before(/\AProp/).each {|lines| p lines }
}
#=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
# ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
# ["Properties on 'complex.c':\n", " svn:mime-type\n", "...

Enumerable#slice_before(pattern) -> Enumerator (49.0)

パターンがマッチした要素、もしくはブロックが真を返した要素から 次にマッチする手前までを チャンク化(グループ化)したものを繰り返す Enumerator を 返します。

...配列として表現されます。

Enumerable#to_a や Enumerable#map のようなメソッドを使うこ
ともできます。

//emlist[例][ruby]{
# 偶数要素をチャンクの先頭と見なす
[0,2,4,1,2,4,5,3,1,4,2].slice_before(&:even?).to_a
# => [[0], [2], [4, 1], [2], [4, 5, 3, 1], [...
...トリーを順に取る
open("ChangeLog") {|f|
f.slice_before(/\A\S/).each {|e| pp e}
}

# 上と同じだが、パターンでなくブロックを使う
open("ChangeLog") {|f|
f.slice_before {|line| /\A\S/ === line }.each {|e| pp e}
}

# "svn proplist -R" の結果を分割する
# これは...
...IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) {|f|
f.lines.slice_before(/\AProp/).each {|lines| p lines }
}
#=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
# ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
# ["Properties on 'complex.c':\n", " svn:mime-type\n", "...
<< < 1 2 3 > >>