I have a class with a custom iterator (each) that uses block.arity to
determine how many arguments to use on the yield. It works well but
when the caller invokes the iterator via each_with_index, the value
of block.arity is -1 which wreaks havoc. Does anyone know how to get the
correct value of block.arity in this situation?\
ruby -v
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Here is the sample code:
#!/usr/bin/ruby -w
test block.arity value with each_with_index
class A
include Enumerable
def each( p, &b )
puts “arity = #{b.arity}”
yield ‘hello’, ‘world’
end
end
Why do you need to check the arity at all? If your method behaves
differently depending on the number of block arguments, it’s a clear
sign you need two different methods.
-----Messaggio originale-----
Da: Bug F. [mailto:[email protected]]
Inviato: mercoled 23 novembre 2011 04:13
A: ruby-talk ML
Oggetto: block.arity when using each_with_index
Hi,
I have a class with a custom iterator (each) that uses block.arity to
determine how many arguments to use on the yield. It works well but when
the
caller invokes the iterator via each_with_index, the value of
block.arity is
-1 which wreaks havoc. Does anyone know how to get the correct value of
block.arity in this situation?\
ruby -v
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Here is the sample code:
#!/usr/bin/ruby -w
test block.arity value with each_with_index
class A
include Enumerable
def each( p, &b )
puts “arity = #{b.arity}”
yield ‘hello’, ‘world’
end
end