Blake
Blake

Reputation: 3

Is multiple include?() arguments in ruby possible?

def coffee_drink?(drink_list)
  drink_list.include?("coffee") ? true : drink_list.include?("espresso") ? true : false
end

I am learning Ruby with TOP and am looking to check for more than a single argument with the include function. I don't think my solution is too bad but surely there is a better solution I am just unable to find.

e.g. include?("ABC", "CBA) and include?("ABC" || "CBA") wont work.

Upvotes: 0

Views: 55

Answers (1)

Ursus
Ursus

Reputation: 30056

def coffee_drink?(drink_list)
  %w(coffee espresso).any? { |drink| drink_list.include?(drink) } 
end

or

def coffee_drink?(drink_list)
  (%w(coffee espresso) & drink_list).any?
end

note that your version could be rewritten like this

def coffee_drink?(drink_list)
  drink_list.include?("coffee") || drink_list.include?("espresso")
end

Upvotes: 3

Related Questions