# Array.include? strangeness

**URL:** <https://www.ruby-forum.com/t/array-include-strangeness/59290>\
**Category:** Ruby\
**Created:** [May 3, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290 "2006-05-03T15:22:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Martin\_Smith](https://www.ruby-forum.com/u/Martin_Smith)\
**Post date:** [May 3, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290/1 "2006-05-03T15:22:23Z")

</div>

Array.include? doesn’t seem to be telling me the truth.

Obviously this is _ME_ getting it wrong, but could somebody explain what  
is happening please.

1. First I set up ab array of common words

def initialize  
@common\_words = ["&", “and”, “or”, “ltd”, “ltd.”, “limited”]  
end

1. Here I check to see if the variable words contains any uncommon  
words, and return them in a new array

def remove\_common\_words words  
ok\_words = Array.new  
words.each(" “) do  
|word|  
logger.debug “checking if word " + word + " is common”  
if !@common\_words.include?(word)  
logger.debug word + " is not common”  
ok\_words \<\< word  
logger.debug "ok\_words: ".concat ok\_words.to\_s  
end  
end

```
return ok_words

```

end

1. And this is the simple test…

def test\_remove\_common\_words  
e = Entity.new  
x = e.remove\_common\_words “limited Martin & Steve’s Company limited”

```
puts x

```

end

The output from the puts is: Uncommon words returned:limited Martin &  
Steve’s Company

---

<div class="post-metadata">

**Author:** ![Martin\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Martin\_Smith](https://www.ruby-forum.com/u/Martin_Smith)\
**Post date:** [May 3, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290/2 "2006-05-03T16:41:54Z")

</div>

Hi,  
See comment below  
Martin Smith wrote:

> Array.include? doesn’t seem to be telling me the truth.
> 
> Obviously this is _ME_ getting it wrong, but could somebody explain what  
> is happening please.
> 
> 1. First I set up ab array of common words
> 
> def initialize  
> @common\_words = ["&", “and”, “or”, “ltd”, “ltd.”, “limited”]  
> end
> 
> 1. Here I check to see if the variable words contains any uncommon  
> words, and return them in a new array
> 
> def remove\_common\_words words  
> ok\_words = Array.new  
> words.each(" ") do  
> ^  
> |-- this operation leaves a space at the end of each word.

> ```
> |word|
> logger.debug "checking if word " + word + " is common"
> if !@common_words.include?(word)
> 
> ```

```
           ^
           |-- replace with '!@common_words.include?(word.strip)' 

```

and it should work fine

> ```
> logger.debug word + " is not common"
> ok_words << word
> logger.debug "ok_words: ".concat ok_words.to_s
> end
> end
> 
> return ok_words
> 
> ```
> 
> end
> 
> 1. And this is the simple test…
> 
> def test\_remove\_common\_words  
> e = Entity.new  
> x = e.remove\_common\_words “limited Martin & Steve’s Company limited”
> 
> ```
> puts x
> 
> ```
> 
> end
> 
> The output from the puts is: Uncommon words returned:limited Martin &  
> Steve’s Company

good luck

Bill Stevens

---

<div class="post-metadata">

**Author:** ![Martin\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Martin\_Smith](https://www.ruby-forum.com/u/Martin_Smith)\
**Post date:** [May 3, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290/3 "2006-05-03T16:57:32Z")

</div>

Thanks William - that worked great.

---

<div class="post-metadata">

**Author:** ![Martin\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Martin\_Smith](https://www.ruby-forum.com/u/Martin_Smith)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290/4 "2006-05-03T19:07:15Z")

</div>

Try this instead:

def remove\_common\_words(words)  
words.split(" ") - @common\_words  
end

---

<div class="post-metadata">

**Author:** ![Martin\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Martin\_Smith](https://www.ruby-forum.com/u/Martin_Smith)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/array-include-strangeness/59290/5 "2006-05-03T19:07:16Z")

</div>

Forgot to say that the problem is that String#each doesn’t do what you  
want.

irb\> “foo bar”.each(" ") { |x| p x }  
"foo "  
“bar”

(Note the space in "foo ".)
