Do I lack understanding or arrays?

H

Hakusa

I'm doing the pascal quiz on rubyquiz.com, but I'm finding that I'm
having trouble assigning a value to my array.

I've assigned it like this:

pyramid_lvl[level] =\
( (pyramid[level-1][i-1]).to_i \
+ (pyramid[level-1] ).to_i )

and like this:
num1 = pyramid[level-1][i-1].to_i
num2 = pyramid[level-1].to_i
ans = num1+num2
pyramid_lvl[level] = [ans]

Both give me this error message:
undefined method `[]=' for 1:Fixnum (NoMethodError)

Am I getting this problem because I don't understand how arrays work
and it's simply a matter of operations, or am I having some serious
logical problem preventing correctness.
 
D

Dan Zwell

I'm doing the pascal quiz on rubyquiz.com, but I'm finding that I'm
having trouble assigning a value to my array.

I've assigned it like this:

pyramid_lvl[level] =\
( (pyramid[level-1][i-1]).to_i \
+ (pyramid[level-1] ).to_i )

and like this:
num1 = pyramid[level-1][i-1].to_i
num2 = pyramid[level-1].to_i
ans = num1+num2
pyramid_lvl[level] = [ans]

Both give me this error message:
undefined method `[]=' for 1:Fixnum (NoMethodError)

Am I getting this problem because I don't understand how arrays work
and it's simply a matter of operations, or am I having some serious
logical problem preventing correctness.


From the error, I would venture to guess (pretty sure) that
pyramid_lvl[level] contains a number, not an array (as you seem to
think). I couldn't say more without reading more code.

Dan
 
D

dblack

Hi --

I'm doing the pascal quiz on rubyquiz.com, but I'm finding that I'm
having trouble assigning a value to my array.

I've assigned it like this:

pyramid_lvl[level] =\
( (pyramid[level-1][i-1]).to_i \
+ (pyramid[level-1] ).to_i )

and like this:
num1 = pyramid[level-1][i-1].to_i
num2 = pyramid[level-1].to_i
ans = num1+num2
pyramid_lvl[level] = [ans]

Both give me this error message:
undefined method `[]=' for 1:Fixnum (NoMethodError)

Am I getting this problem because I don't understand how arrays work
and it's simply a matter of operations, or am I having some serious
logical problem preventing correctness.


It's just what the error says: you're trying to call the method []= on
the object 1 :) That means that pyramid_lvl[level] must be 1. So
when you do:

pyramid_lvl[level] = whatever

it's like you're doing:

1 = whatever

That's equivalent to a method call:

1[]=(i, whatever)

and 1 has no method called []= so you get an error.

I can't tell you where in your code you assigned 1 to
pyramid_lvl[level], but that appears to be what's happening.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
H

Hakusa

I tested this:

pyramid_lvl[level] is 0
pyramid[level-1] is 1
pyramid[level-1][i-1] is 1

Turns out that pyramid[level-1] was actually nil... which makes
this a logical error, not a syntax error.

So I pretty much just waisted your time, but so long and thanks for
all the fish.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top