.include?/.within? and English-thinking brains

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.

HI –

On Tue, 26 Jun 2007, Michael J. wrote:

In English, it’s clearer to ask, “Is Maggie one of the guests?” – rather
than the awkward, “Does the guests include Maggie?”

Well, you’re stacking the deck a bit to make it sound awkward (“Does
the guests”) :slight_smile: Anyway, we have both; it all depends what you’re
trying to say.

So why in Ruby, do we write, “list.include? scalar” – when we often mean,
“scalar.within? list” ?!

Because Ruby isn’t English; we use both in English anyway; and Ruby
doesn’t have #within? :slight_smile:

The solution:

module Comparable
def within?(list)
list.include? self
end
end

There’s been discussion of this in the past; have a look for #in?
proposed by Hal a while back.

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs