McBilly McBilly - 1 year ago 67
Ruby Question

How to set an IF statement comparing to values inside an array? (Ruby)

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
# do another thing

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
# capitalize word

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

Thank you!

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download