Home > Blockchain >  Is multiple include?() arguments in ruby possible?
Is multiple include?() arguments in ruby possible?

Time:09-16

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.

CodePudding user response:

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
  • Related