るりまサーチ

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

別のキーワード

  1. _builtin to_s
  2. openssl to_der
  3. openssl to_s
  4. _builtin to_a
  5. openssl to_pem

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 > >>

Object#to_enum(method = :each, *args) -> Enumerator (15145.0)

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

...数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param 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...
...1, 2, 3]
p
(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__metho...
...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
//}

@
see Enumerator, Enumerator#size...

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

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

...数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param 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...
...1, 2, 3]
p
(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__metho...
...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
//}

@
see Enumerator, Enumerator#size...

Enumerator#peek -> object (6161.0)

「次」のオブジェクトを返しますが、列挙状態を変化させません。

...達している場合は、StopIteration 例外を発生します。

//emlist[例][ruby]{
a = [1,2,3]
e = a.to_enum
p
e.next #=> 1
p
e.peek #=> 2
p
e.peek #=> 2
p
e.peek #=> 2
p
e.next #=> 2
p
e.next #=> 3
p
e.next #raises StopIteration
//}

@
raise StopIteration 列挙状態が既に...
...最後へ到達しているとき
@
see Enumerator#next, Enumerator#next_values, Enumerator#peek_values...

Enumerator#peek_values -> Array (6149.0)

Enumerator#next_values のように「次」のオブジェクトを 配列で返しますが、列挙状態を変化させません。

...StopIteration 例外を発生します。

このメソッドは Enumerator#next_values と同様
yield

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

//emlist[例][ruby]{
o = Object.new
def o.each
yield
yield 1
yield 1, 2
end
e = o.to_enum
p
e.peek_values #=> []
e.next
p
e.peek...
..._values #=> [1]
p
e.peek_values #=> [1]
e.next
p
e.peek_values #=> [1, 2]
e.next
p
e.peek_values # raises StopIteration
//}

@
raise StopIteration 列挙状態が既に最後へ到達しているとき
@
see Enumerator#next, Enumerator#next_values, Enumerator#peek_values...

Enumerable#compact -> Array (6113.0)

self から nil を取り除いた配列を生成して返します。

...self から nil を取り除いた配列を生成して返します。

//emlist[][ruby]{
def with_nils
yield 1
yield 2
yield nil
yield 3
end

to_enum
(:with_nils).compact # => [1, 2, 3]
//}

@
see Array#compact...

絞り込み条件を変える

Enumerator#next_values -> Array (85.0)

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

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

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

このメソッドは、
yield

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

next メソッド...
...w
def o.each
yield
yield 1
yield 1, 2
yield nil
yield [1, 2]
end
e = o.to_enum
p
e.next_values
p
e.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 []...
...[1] 1
# yield 1, 2 [1, 2] [1, 2]
# yield nil [nil] nil
# yield [1, 2] [[1, 2]] [1, 2]
//}

@
raise StopIteration 列挙状態が既に最後へ到達しているとき
@
see Enumerator#next, Enumerator#peek, Enumerator#peek_values...

Object#enum_for(method = :each, *args) -> Enumerator (45.0)

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

...数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param 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...
...1, 2, 3]
p
(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__metho...
...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
//}

@
see Enumerator, Enumerator#size...

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

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

...数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param 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...
...1, 2, 3]
p
(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__metho...
...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
//}

@
see Enumerator, Enumerator#size...

Enumerator#each -> self (37.0)

生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。

...したイテレータの戻り値をそのまま返します。

@
param args 末尾へ追加する引数

//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"

enum = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
enum.each {|word| p word } # => "Yet"...
...# "Another"
# "Ruby"
# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "Ruby"...
...orld"]
"Hello, world!".to_enum(:scan, /\w+/).to_a # => ["Hello", "world"]
"Hello, world!".to_enum(:scan).each(/\w+/).to_a # => ["Hello", "world"]

obj = Object.new

def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end

enum = obj.to_enum :each_arg, :a, :x

en...
<< 1 2 > >>