Code Block Error

D

Drew Olson

The following code works fine:

counter = 0;
semi_common_tbls.keys.each do |set|
semi_common_tbls[set].each do |table|
outfile << ["CommonTables"+counter.to_s,table]
end
end

However, when I change it like so:

counter = 0;
semi_common_tbls.keys.each do |set|
counter++
semi_common_tbls[set].each do |table|
outfile << ["CommonTables"+counter.to_s,table]
end
end

I get the following error:
tablebuildv2.rb:46: undefined method `+@' for ["table1", "table5"]:Array
(NoMethodError)
from tablebuildv2.rb:42:in `each'
from tablebuildv2.rb:42

What gives?

Thanks,
Drew
 
J

James Edward Gray II

However, when I change it like so:

counter = 0;
semi_common_tbls.keys.each do |set|
counter++

Ruby doesn't have a ++ operator. Try:

counter += 1
semi_common_tbls[set].each do |table|
outfile << ["CommonTables"+counter.to_s,table]
end
end

I get the following error:
tablebuildv2.rb:46: undefined method `+@' for ["table1",
"table5"]:Array
(NoMethodError)
from tablebuildv2.rb:42:in `each'
from tablebuildv2.rb:42

James Edward Gray II
 
C

ChrisH

Drew said:
However, when I change it like so:

counter = 0;
semi_common_tbls.keys.each do |set|
counter++

'++' is not a Ruby operator, it doesn't have any of the
unary increment/decrement operators.

Closest is
counter += 1

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top