11175
(-- --)
1
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
Jeffrey
(Jeffrey)
2
Quoting First Bat <rails-mailing-list@andreas-s.net>:
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
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
11175
(-- --)
3
First Bat 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?
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.