"No block given" error -- spurious?

The code below, found at
http://github.com/eregon/RPCFN4/blob/master/solutions/benoit_daloze.rb
(with
the last two lines added for testing it) produced the error:
Polynomials.rb:15:in `each_with_index’: no block given (LocalJumpError)
The erronious line is flagged with #=================
Debugging indicates that @ceof is an Array.
What’s up?

Benoit D.

Ruby P.ming Challenge For Newbies #4

pretty-print polynomials

My solution use Enumerable#inject to build the String,

with every “monomial” splitted in 3 parts : sign, coefficient and

variable
class Polynomial
def initialize(coef)
raise ArgumentError, “Need at least 2 coefficients” if coef.size < 2
@coef = coef
end

def to_s
return ‘0’ if @coef.all? { |c| c == 0 }
@coef.each_with_index.inject(“”) { |s, (coef, i)| #
<===================================
pow = @coef.length-1-i
if coef == 0
s
else
s +
(coef > 0 ? ‘+’ : ‘-’) + # sign
(coef.abs == 1 && pow != 0 ? ‘’ : coef.abs.to_s) + # coef
(pow < 2 ? ‘x’*pow : “x^#{pow}”) # var
end
}.sub(/^+/, “”)
end
end

p = Polynomial.new( [1, 2] )
puts p

— news://freenews.netfront.net/ - complaints: [email protected]

On Dec 24, 12:08 pm, “Richard L.” [email protected] wrote:

@coef.each_with_index.inject("") { |s, (coef, i)|   #
}.sub(/^\+/, "")

end
end

p = Polynomial.new( [1, 2] )
puts p

— news://freenews.netfront.net/ - complaints: [email protected]
This should work with Ruby 1.9, as each_with_index returns an
Enumerator when called without a block. Ruby 1.8’s each_with_index,
however, requires a block to be passed to it.

On Dec 24, 12:47 pm, pharrington [email protected] wrote:

Ruby P.ming Challenge For Newbies #4

def to_s
(pow < 2 ? ‘x’*pow : “x^#{pow}”) # var
This should work with Ruby 1.9, as each_with_index returns an
Enumerator when called without a block. Ruby 1.8’s each_with_index,
however, requires a block to be passed to it.

Thanks a lot. And I apologize for not mentioning that I was running
1.8.6.