# Strange error in ruby 1.9.1

**URL:** <https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744>\
**Category:** Ruby\
**Created:** [May 17, 2010, 5:52pm UTC](https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744 "2010-05-17T17:52:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brandentanga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brandentanga](https://www.ruby-forum.com/u/brandentanga)\
**Post date:** [May 17, 2010, 5:52pm UTC](https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744/1 "2010-05-17T17:52:26Z")

</div>

Hello all,

I just compiled ruby 1.9.1 from source on my computer (osx snow  
leopard), and I’m going through the old ruby quiz exercises for fun. I’m  
on the secret santa one, and I wrote the code that determines that each  
persons secret santa does not have the same last name, to ensure that  
each person has to get a gift for someone from a different family. The  
quiz is listed here:

[http://rubyquiz.com/quiz2.html](http://rubyquiz.com/quiz2.html)

My code runs fine most of the time. However, occasionally I’ll get a  
strange “undefined method” error. See the code below and sample output:

class Person  
attr\_accessor :first, :last, :recipient  
def initialize(first, last)  
@first = first  
@last = last  
@recipient = nil  
end  
end

a = [giant listing of names in the format of “first last”]

santa\_array = [] # instantiate an empty array  
a.each do |full\_name|  
split\_array = full\_name.split  
my\_person = Person.new(split\_array[0], split\_array[1])  
santa\_array.push(my\_person)  
end

recipient\_array = Array.new(santa\_array)  
santa\_array.shuffle!  
recipient\_array.shuffle!

santa\_array.each do |element|  
index = 0  
while element.recipient == nil  
if element.last != recipient\_array[index].last # if the last names  
are different, process  
element.recipient = recipient\_array[index]  
recipient\_array.delete\_at(index)  
else  
index += 1 # else the last names are the same, loop again on index  
+= 1  
end  
end  
end

santa\_array.each { |element| puts "#{element.first} #{element.last},  
recipient == #{element.recipient.first} " +  
“#{element.recipient.last}” }  
puts “\n\n”  
puts “santa\_array.count == #{santa\_array.count}”  
puts “recipient\_array.count == #{recipient\_array.count}”

And here’s some sample output:  
brandentanga$ ./secretSanta.rb  
./secretSanta.rb:67:in `block in <main>': undefined method `last’ for  
nil:NilClass (NoMethodError)  
from ./secretSanta.rb:64:in `each' from ./secretSanta.rb:64:in `’  
brandentanga$ ./secretSanta.rb  
luke skywalker, recipient == lando calrissian  
darth vader, recipient == han solo  
anakin skywalker, recipient == jeanluc piccard  
… so on and so forth, continuing to print properly…

As you can see from the above output, I ran my code twice, with no  
modifications, all names are hard coded, and guaranteed to have first  
and last names separated by a space. So how can the same code with the  
same input produce a runtime error, then on the next run process as  
expected?

---

<div class="post-metadata">

**Author:** ![brandentanga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brandentanga](https://www.ruby-forum.com/u/brandentanga)\
**Post date:** [May 17, 2010, 6:11pm UTC](https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744/2 "2010-05-17T18:11:32Z")

</div>

On Tue, 18 May 2010 00:52:31 +0900, Branden T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> recipient\_array = Array.new(santa\_array)  
> else  
> puts “santa\_array.count == #{santa\_array.count}”  
> darth vader, recipient == han solo  
> anakin skywalker, recipient == jeanluc piccard  
> … so on and so forth, continuing to print properly…
> 
> As you can see from the above output, I ran my code twice, with no  
> modifications, all names are hard coded, and guaranteed to have first  
> and last names separated by a space. So how can the same code with the  
> same input produce a runtime error, then on the next run process as  
> expected?

You don’t check that index \< recipient\_array.size so if enough last  
names  
match recipient\_array[index] will be out of bounds. In Ruby Array.[]  
returns nil for out of bounds indices. To answer your exact question the  
random order given by shuffle! will be different each time you run the  
program so although the input is the same the result of your algorithm  
is  
not.

---

<div class="post-metadata">

**Author:** ![brandentanga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brandentanga](https://www.ruby-forum.com/u/brandentanga)\
**Post date:** [May 17, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744/3 "2010-05-17T17:59:03Z")

</div>

> And here’s some sample output:  
> brandentanga$ ./secretSanta.rb  
> ./secretSanta.rb:67:in `block in <main>': undefined method`last’ for  
> nil:NilClass (NoMethodError)

bug maybe? try trunk and see if it does the same…  
-rp

---

<div class="post-metadata">

**Author:** ![brandentanga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brandentanga](https://www.ruby-forum.com/u/brandentanga)\
**Post date:** [May 17, 2010, 6:58pm UTC](https://www.ruby-forum.com/t/strange-error-in-ruby-1-9-1/188744/4 "2010-05-17T18:58:16Z")

</div>

Alex G. wrote:

> You don’t check that index \< recipient\_array.size so if enough last  
> names  
> match recipient\_array[index] will be out of bounds. In Ruby Array.[]  
> returns nil for out of bounds indices. To answer your exact question the  
> random order given by shuffle! will be different each time you run the  
> program so although the input is the same the result of your algorithm  
> is  
> not.

Aaaah, that makes perfect sense. Thanks for the help.
