Prevent duplicate words in array

hello,

how do i prevent someone to enter duplicate word

for example,

eat = “”
data = []

print "what did you eat for dinner?: "
eat = gets.chomp

data << eat

so let say if they enter a food twice, it would print something like

" #{eat} you already entered that"

i was told to use Array#index, but i have no idea how to use it

thank you

Quoting First B. [email protected]:

data = []
i was told to use Array#index, but i have no idea how to use it

eat = “”
data = {}
print "what did you eat for dinner?: "
eat = gets.chomp

if data[eat]
puts " #{eat} you already entered that"
else
data[eat] = true
end

HTH,
Jeffrey

Hi

data = []
eat = gets.chomp
data.include?(eat) ?"#{eat}you already entered that":data<< eat

Sijo

A similar method would be to use Array#uniq! ( or maybe uniq)

data = []
data << gets.chomp
data.uniq!

Many cats and oh so many ways to skin them.

On Oct 21, 5:04 am, Aldric G. <rails-mailing-l…@andreas-

First B. wrote:

hello,

how do i prevent someone to enter duplicate word

for example,

irb(main):024:0> require ‘set’
=> []
irb(main):025:0> a = Set.new
=> #<Set: {}>
irb(main):026:0> a << “melon”
=> #<Set: {“melon”}>
irb(main):027:0> a << “apple”
=> #<Set: {“apple”, “melon”}>
irb(main):028:0> a << “melon”
=> #<Set: {“apple”, “melon”}>

Yes, no?