简体   繁体   中英

Iterating over an array in ruby

I have an array

array = ["this","is","a","sentence"]

I want to print a string if one of the words in array matches a word I'm looking for.

Example:

array = ["this","is","a","sentence"]

array.each { |s|
  if 
    s == "sentence"
    puts "you typed the word sentence."
  elsif 
    s == "paragraph"
    puts "You typed the word paragraph."
  else
    puts "You typed neither the words sentence or paragraph."
  end

This method will print:

  "You typed neither the words sentence or paragraph."
  "You typed neither the words sentence or paragraph."
  "You typed neither the words sentence or paragraph."
  "you typed the word sentence."

I want it to recognize the word "sentence" and execute "you typed the word sentence." . If one of those words is not present, it will execute the else statement "you typed neither the words sentence or paragraph." .

You'll want to check the array using include? :

array = ["this","is","a","sentence"]

if array.include?("sentence")
  puts "You typed the word sentence."
elsif array.include?("paragraph")
  puts "You typed the word paragraph."
else
  puts "You typed neither the words sentence or paragraph."
end

1.9.3p448 :016 > array = ["this","is","a","sentence"]
 => ["this", "is", "a", "sentence"] 
1.9.3p448 :017 > 
1.9.3p448 :018 >   if array.include?("sentence")
1.9.3p448 :019?>     puts "You typed the word sentence."
1.9.3p448 :020?>   elsif array.include?("paragraph")
1.9.3p448 :021?>     puts "You typed the word paragraph."
1.9.3p448 :022?>   else
1.9.3p448 :023 >     puts "You typed neither of the words sentence or paragraph."
1.9.3p448 :024?>   end
You typed the word sentence.

The basic problem that makes this seem tricky is that you're combining the act of finding the word (looping through the array) with the what you want to do with the word once you've found it.

A more idiomatic way to write this would separate them:

array = ["this","is","a","sentence"]

found = array.find {|word| word == 'sentence' || word == 'paragraph' }

case found
  when 'sentence' then puts 'You typed the word sentence'
  when 'paragraph' then puts 'You typed the word paragraph'
  else puts "You typed neither the words sentence or paragraph"
end

Seems like you're splitting the user's input. You could use regular expressions to find the matches instead:

input = "this is a sentence"

case input
when /sentence/
  puts "You typed the word sentence"
when /paragraph/
  puts "You typed the word paragraph"
else
  puts "You typed neither the words sentence or paragraph"
end

As noted by theTinMan, you have to surround the pattern with \\b (matches a word boundary ) in order to match whole words:

/sentence/     === "unsentenced" #=> true
/\bsentence\b/ === "unsentenced" #=> false
/\bsentence\b/ === "sentence"    #=> true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM