How to write an "if in" statement in Ruby
arrays, if-statement, ruby
Solution
if line.include?(destination) && line.include?(location)
if [destination,location].all?{ |o| line.include?(o) }
if ([destination,location] & line).length == 2
The first is the most clear, but least DRY.
The last is the least clear, but fastest when you have multiple items to check. (It is `O(m+n)` vs `O(m*n)`.)
I'd personally use the middle one, unless speed was of paramount importance.
Problem
I'm looking for and if-in statement like Python has for Ruby. Essentially, if x in an_array do This is the code I was working on, where the variable "line" is an array. ``` def distance(destination, location, line) if destination and location in line puts "You have #{(n.index(destination) - n.index(location)).abs} stops to go" end end ```