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 -- Posted via http://www.ruby-forum.com/.
Quoting First Bat <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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
Hi data = [] eat = gets.chomp data.include?(eat) ?"#{eat}you already entered that":data<< eat Sijo -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
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 Giacomoni <rails-mailing-l...@andreas- s.net> wrote:> 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? > -- > Posted viahttp://www.ruby-forum.com/.