Forum: Ruby Do I lack understanding or arrays?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-27 22:36
(Received via mailing list)
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][i] =\
          ( (pyramid[level-1][i-1]).to_i \
          + (pyramid[level-1][i]  ).to_i )

and like this:
      num1 = pyramid[level-1][i-1].to_i
      num2 = pyramid[level-1][i].to_i
      ans = num1+num2
      pyramid_lvl[level][i] = [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.
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-05-27 23:12
(Received via mailing list)
Hakusa@gmail.com wrote:
>       num1 = pyramid[level-1][i-1].to_i
>
>
>

 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-27 23:14
(Received via mailing list)
Hi --

On Mon, 28 May 2007, Hakusa@gmail.com wrote:

>      num1 = pyramid[level-1][i-1].to_i
>      num2 = pyramid[level-1][i].to_i
>      ans = num1+num2
>      pyramid_lvl[level][i] = [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][i] = whatever

it's like you're doing:

   1[i] = 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
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-28 01:41
(Received via mailing list)
I tested this:

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

Turns out that pyramid[level-1][i] 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.
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-28 01:42
(Received via mailing list)
> was actually nil
typo. I meant 0
This topic is locked and can not be replied to.