0

Is it possible to set a conditional statement (IF statement) comparing a variable against a variable that iterates through the values inside an array? I was looking for something like:

array_of_small_words = ["and","or","be","the","of","to","in"]
if word == array_of_small_words.each 
   # do thing
else
   # do another thing
end

Basically, I want to capitalize each word but don't want to do it for "small words". I know I could do the the opposite and iterate through the array first and then compare each iteration with the word but I was hoping there would be a more efficient way.

sentence = ["this","is","a","sample","of","a","title"]
array_of_small_words = ["and","or","be","the","of","to","in"]
sentence.each do |word|
    array_of_small_words.each do |small_words|
       if word == small_words
          # don't capitalize word
       else
          # capitalize word
       end
    end
end

I'm not really sure if this is possible or if there is a better way of doing this?

Thank you!

4
  • Why the rush to select an answer? Perhaps you wish to discourage others from giving answers. There's no hurry, you know. Commented Jul 30, 2016 at 19:21
  • Hi Cary. Sorry. Discouraging others from giving their answers was definitely not my intent. I'm new here and still getting used to the flow of how things go here so I hope you can bare with me. I appreciate your feedback! I removed the "selected answer". Thank you! Commented Jul 30, 2016 at 20:10
  • That was just how I chose to make the point :-), but I see how it could be misinterpreted. Another reason for holding off is that some readers may find it a bummer to see the ✅ flash on while they are preparing their answers. Many here wait at least a couple of hours, often much longer. Have a look at the SO faq when you have the time. Commented Jul 30, 2016 at 20:35
  • Thanks a lot Cary! :) Definitely see your point on holding off on selecting answered. I'm checking out that SO faq you linked to. Honestly, I'm very new to coding and so I haven't really been introduced to the "deeper" syntax and concepts. Anyway, thanks so much for you answer! I'm sure it will help others when they reach this question. :-) Commented Jul 31, 2016 at 3:42

3 Answers 3

1
sentence = ["this","is","a","sample","of","a","title"]
array_of_small_words = ["and","or","be","the","of","to","in"]

sentence.map do |word|
  array_of_small_words.include?(word) ? word : word.upcase
end
#⇒ ["THIS", "IS", "A", "SAMPLE", "of", "A", "TITLE"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I haven't been introduced to the map method yet and I will look into this.
Readers, if packaged in a method and frequently used, this could be sped up by: require 'set'; ARRAY_OF_SMALL_WORDS = ["and",..,"in"], then ARRAY_OF_SMALL_WORDS.include?(word) ....
0

What you're looking for is if array_of_small_words.include?(word).

3 Comments

Thanks a lot! This is exactly what I was looking for.
McBilly, this may be all you're looking for, but it doesn't answer the question. Answers need to show how the array ["THIS", "IS", "A", "SAMPLE", "of", "A", "TITLE"] is produced.
Hi Cary. Sorry. I'm new here and still getting used to how things are done. You're right. I removed the "selected answer". Thanks for the feedback!
0

This should be faster than @mudasobwa's repeated use of include? if packaged in a method and used frequency. It would not be faster, however, if mudsie used a set lookup (a minor change, of which he is well-aware), as I mentioned in a comment. If efficiency is important, I'd prefer mudsie's way with the set mod over my answer. In a way I was just playing around below.

I've assumed he small words are and, or, be, the, of, to, in and notwithstanding.

SMALL_WORDS = %w| and or be the of to in notwithstanding |
  #=> ["and", "or", "be", "the", "of", "to", "in", "notwithstanding"] 
(SMALL_WORDS_HASH = SMALL_WORDS.map { |w| [w.upcase, w] }.to_h).
  default_proc = proc { |h,k| h[k]=k }

Test:

SMALL_WORDS_HASH
  #=> {"AND"=>"and", "OR"=>"or", "BE"=>"be", "THE"=>"the", "OF"=>"of",
  #    "TO"=>"to", "IN"=>"in", "NOTWITHSTANDING"=>"notwithstanding"} 
SMALL_WORDS_HASH["TO"]
  #=> "of" 
SMALL_WORDS_HASH["HIPPO"]
  #=> "HIPPO" 

def convert(arr)
  arr.join(' ').upcase.gsub(/\w+/, SMALL_WORDS_HASH)
end

convert ["this","is","a","sample","of","a","title"]
  #=> "THIS IS A SAMPLE of A TITLE" 

Comments

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.