Michael Judge
6/25/2007 8:54:00 PM
In English, it's clearer to ask, "Is Maggie one of the guests?" --
rather than the awkward, "Does the guests include Maggie?"
So why in Ruby, do we write, "list.include? scalar" -- when we often
mean, "scalar.within? list" ?!
The solution:
module Comparable
def within?(list)
list.include? self
end
end
#
# Examples:
#
1.within? [1,2,3]
# => true
1.within? [2,3]
# => false
"arr".within? "arr, matey"
# => true
"arr".within? "monkey patch"
# => false
1.within?( 1 => "value" )
# => true
1.within?( 2 => "value" )
# => false
1.within? 1..3
# => true
1.within? 2..3
# => false
Try it for a morning, your brain will feel better.