Reputation: 1102
a = [1, 2, 3..9, 10, 15, 20..43]
print a.include?(5) # Returns false
I was expecting it to return true
, but 3..9
is not translated to [3,4,5,6,7,8,9]
.
I am missing something silly but I can't figure it out. Basically I want to initialize it with both regular fixnums and ranges.
Upvotes: 1
Views: 132
Reputation: 6672
If you would like a "lazier" approach that doesn't require you to convert ranges into array elements, try using the ===
(case equality) operator.
a = [1, 2, 3..9, 10, 15, 20..43]
a.any? { |x| x === 5 }
I recommend using this approach since it's far more efficient than splatting the range into separate elements.
Upvotes: 3
Reputation: 29124
Another solution, without splat.
a = [1, 2, 3..9, 10, 15, 20..43]
a.any? {|i| i.kind_of?(Range) ? i.include?(5) : i == 5 }
# => true
Upvotes: 1
Reputation: 118271
You have to splat it
a = [1, 2, *3..9, 10, 15, 20..43]
a.include?(5) # => true
Upvotes: 7