別のキーワード
種類
- インスタンスメソッド (88)
- モジュール関数 (22)
- 文書 (11)
ライブラリ
- ビルトイン (110)
クラス
-
Enumerator
:: Lazy (44) - Object (44)
モジュール
- Kernel (22)
キーワード
-
enum
_ for (44) - iterator? (11)
-
to
_ enum (44) - クラス/メソッドの定義 (11)
検索結果
先頭5件
-
Kernel
. # block _ given? -> bool (24350.0) -
メソッドにブロックが与えられていれば真を返します。
...ートするとはいえないので)推奨されていないの
で block_given? を使ってください。
//emlist[例][ruby]{
def check
if block_given?
puts "Block is given."
else
puts "Block isn't given."
end
end
check{} #=> Block is given.
check #=> Block isn't given.
//}... -
Kernel
. # iterator? -> bool (6150.0) -
メソッドにブロックが与えられていれば真を返します。
...ートするとはいえないので)推奨されていないの
で block_given? を使ってください。
//emlist[例][ruby]{
def check
if block_given?
puts "Block is given."
else
puts "Block isn't given."
end
end
check{} #=> Block is given.
check #=> Block isn't given.
//}... -
Enumerator
:: Lazy # enum _ for(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (214.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...素を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}... -
Enumerator
:: Lazy # to _ enum(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (214.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...素を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}... -
クラス/メソッドの定義 (186.0)
-
クラス/メソッドの定義 * クラス/メソッドの定義: * class * singleton_class * module * method * operator * nest_method * eval_method * singleton_method * class_method * limit * 定義に関する操作: * alias * undef * defined
...uby]{
def fact(n)
if n == 1 then
1
else
n * fact(n-1)
end
end
//}
文法:
def メソッド名 ['(' [arg0 ['=' default0]] ... [',' '*' rest_args [, post ...]] [',' key1: [val1]] ... [',' '**'kwrest] [',' '&' block_arg]`)']
式.. (body)
[rescue [error_ty......を省いてdef foo a, bとも
a + 3 * b
end
//}
メソッド名としては通常の識別子の他に、再定義可能な演算子(例: ==, +, -
など spec/operator を参照)も指定できます(operator参照)。
//emlist[例][ruby]{
class Vector2D
attr_accessor :x, :y # インスタ......c0@-:16>
//}
//emlist[例: イテレータの定義][ruby]{
# yield を使う
def foo
# block_given? は、メソッドがブロックを渡されて
# 呼ばれたかどうかを判定する組み込み関数
if block_given?
yield(1,2)
end
end
# Proc.new を使う
def bar
if block_g... -
Enumerator
:: Lazy # enum _ for(method = :each , *args) -> Enumerator :: Lazy (114.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...素を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}... -
Enumerator
:: Lazy # to _ enum(method = :each , *args) -> Enumerator :: Lazy (114.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...素を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}... -
Object
# enum _ for(method = :each , *args) -> Enumerator (112.0) -
Enumerator.new(self, method, *args) を返します。
...(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__method__, n) do
# size メソッドが nil でなければ size * n... -
Object
# enum _ for(method = :each , *args) {|*args| . . . } -> Enumerator (112.0) -
Enumerator.new(self, method, *args) を返します。
...(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__method__, n) do
# size メソッドが nil でなければ size * n... -
Object
# to _ enum(method = :each , *args) -> Enumerator (112.0) -
Enumerator.new(self, method, *args) を返します。
...(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここでは :repeat
return to_enum(__method__, n) do
# size メソッドが nil でなければ size * n...