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

M

Michael Judge

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.
 
D

dblack

HI --

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") :) 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? :)
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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top