# Do I lack understanding or arrays?

**URL:** <https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238>\
**Category:** Ruby\
**Created:** [May 27, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238 "2007-05-27T22:36:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 27, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238/1 "2007-05-27T22:36:56Z")

</div>

I’m doing the pascal quiz on [rubyquiz.com](http://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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 27, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238/2 "2007-05-27T23:12:58Z")

</div>

removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 27, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238/3 "2007-05-27T23:14:58Z")

</div>

Hi –

On Mon, 28 May 2007, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 28, 2007, 1:41am UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238/4 "2007-05-28T01:41:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 28, 2007, 1:42am UTC](https://www.ruby-forum.com/t/do-i-lack-understanding-or-arrays/105238/5 "2007-05-28T01:42:13Z")

</div>

> was actually nil  
> typo. I meant 0
