# Array.include? issue

**URL:** <https://www.ruby-forum.com/t/array-include-issue/152717>\
**Category:** Ruby\
**Created:** [November 6, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/array-include-issue/152717 "2008-11-06T19:35:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [November 6, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/array-include-issue/152717/1 "2008-11-06T19:35:12Z")

</div>

I have the following method defined within a class.

def move  
@heading ||= ‘\>’  
@history \<\< [@x,@y,@heading]  
case @heading  
when ‘\>’  
@y += 1  
when ‘\<’  
@y -= 1  
when ‘^’  
@x -= 1  
else  
@x += 1  
end  
error\_out(“Infinite Loop”) if @history.include?([@x,@y,@heading])  
end

which works as expected on my linux machine. However, when I try the  
same code on OS X the

```
error_out("Infinite Loop") if @history.include?([@x,@y,@heading])

```

fails to execute correctly. In other words, even if [@x,@y,@heading]  
exists in @history the error\_out is never executed. I have verified that  
it does exist by printing the contents of the @history array before the  
check.  
This is a single threaded application and in no other place is @history  
modified.  
I first ran on OS X with ruby version 1.8.2 and then installed 1.8.7  
with the same results. I am running version 1.8.7 on my linux machine.  
Is there an obvious thing that I am missing here?

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [November 6, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/array-include-issue/152717/2 "2008-11-06T22:12:31Z")

</div>

-j b- wrote:

> However, when I try the  
> same code on OS X the
> 
> ```
> error_out("Infinite Loop") if @history.include?([@x,@y,@heading])
> 
> ```
> 
> fails to execute correctly. In other words, even if [@x,@y,@heading]  
> exists in @history the error\_out is never executed. I have verified that  
> it does exist by printing the contents of the @history array before the  
> check.

How did you print it? If it were me I’d add

STDERR.puts @history.inspect  
STDERR.puts [@x, @y, @heading]

just before the error\_out line. What does this show?

Could it be that error\_out() really is being called, but is not behaving  
the way you expect? Try changing to

raise “Infinite loop” if @history.include?([@x,@y,@heading])

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [November 7, 2008, 9:54am UTC](https://www.ruby-forum.com/t/array-include-issue/152717/3 "2008-11-07T09:54:52Z")

</div>

Brian C. wrote:

> STDERR.puts @history.inspect  
> STDERR.puts [@x, @y, @heading]

Oops, add .inspect at the end of the second line.
