0

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

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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

1 Comment

Because the user is matching on words, the pattern needs to duplicate that. By themselves regex are sub-string matches, not word matches. Surround the pattern with \b to make it a word match: /\bfoo\b. Because the search string doesn't have to be split, a regex based search can be very fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.