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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With