Forum: Ruby Array.include? strangeness

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.
74592905ef38cd087e5b73c0955aaa18?d=identicon&s=25 Martin Smith (Guest)
on 2006-05-03 15:22
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

2. 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

3. 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
453336cfba0446c4eb97ee925324517d?d=identicon&s=25 William Stevens (wfspoet)
on 2006-05-03 16:41
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
>
> 2. 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
>
> 3. 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
74592905ef38cd087e5b73c0955aaa18?d=identicon&s=25 Martin Smith (Guest)
on 2006-05-03 16:57
Thanks William - that worked great.
A19281bdbc5f08539cdef3d6636f7c4d?d=identicon&s=25 Christoffer Sawicki (Guest)
on 2006-05-03 19:07
(Received via mailing list)
Try this instead:

def remove_common_words(words)
  words.split(" ") - @common_words
end
A19281bdbc5f08539cdef3d6636f7c4d?d=identicon&s=25 Christoffer Sawicki (Guest)
on 2006-05-03 19:07
(Received via mailing list)
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 ".)
This topic is locked and can not be replied to.