Two new method names relevant to Range #
Matz wants good names for two new method on his blog(2005-12-10).
One is the method to check whether a Range object contains an element or not. It compares by magnitude relation.
(beg..end).foo?(x)
This method would return true if “beg <= x <= end”. In 1.8, the “include?” takes on this role. In 1.9, “include?” means containing in discrete set – except Number. Matz takes a fancy for “cover?”, but he would change his mind if you suggested him a better name. Matz likes a short name for convenient method.
Another method is one to check whether an object is conteined in the “succ” set between “beg” and “end” or not.
x.bar?(beg, end)
This method would return true if “x” were contained, and return false if “x” were not contained or “succ” could’t reach from “beg” to “end”. Other methods such as “include?” use this for efficiency. Probably, we will not use directly. So, it doesn’t matter how long the name is. This method and “between?” is alike, but “Comparable#between?” compares by magnitude relation. The new method is discrete.
Please see the thread: [ruby-talk:167182]
Kreiger
Obviously, the first should be “contains?”, as that’s the word you use.
The other might be “in?”
Jo Vermeulen
Indeed, “contains” seems the most logical choice for me too.
phil602
Wait, include? is going away for numbers?
Console me in my sorrow!
in operator
if x in beg..end
phil602
Scratch that—I read the thread and I can see why include? is ambiguous.
slumos
I like (beg..end).has?(x), which I see has already been suggested even though I can’t read the rest.
slumos
I see too, matz doesn’t like contains? or has? because a Range doesn’t actually contain things. Please see the whole thread next time, slumos.
rosco
Maybe this doesn’t fit too well with other stuff, but it kind of makes sense to me …
(example with 1.8)
bound
(beg..end).bounds?(x) x.bounded_by?(beg..end)
Ezra
(beg..end).covers?(x)
flgr
I’d call the first range.enclose?(item).
I’m not sure about the second—what’s wrong about .include?()?
murphy
Thank you for translating!
Remeber that method names should not be conjugated – so
contain
without s.What about this:
“Surround” is one of my favorite English words :)
murphy
Please correct me if I’m wrong. There is some confusion here. The following is what I understood so far:
The new methods are just counterparts for the ones you likely expect behind the names. Matz wants to make Ruby more intuitively with this addition.
I take cover? and found_between?, just for now.
No problem for numbers:
But with strings, it’s now:
And now we need two methods that do it the other way: a comparison include? and a discreete between?:
I hope this clarifies my and your understanding of this advanced math philosophic stuff. Names are important.
choonkeat
“within range” is quite appropriate. However (beg..end).within? sounds the other way.. unless it acn be x.within? Range
babie
In 1.9, “include?” checks Number by magnitude relation.
undees
murphy, I like “bound?”. I admit I haven’t read the thread, so I don’t know if there’s some kind of ueber-elite reason we can’t use it. All the same, I like “bound?”.
trans
If member?/include? go back to checking a #succ “hit” then
Don’t bother calling me when the calc is finished. I’ll be dead. Of course you might insist it will be optimized to account for numbers, but then you defeat the generalized usage of Range and might as well not bother with the sentinel-based behavior at all. How can one deal with an abstract class that might have a similar time intensive situation, even though there is a very simple and quick calculation to determine membership. There’s the real issue, the sentinel needs a way to provide a succ-based membership comparitor (it can be optional).
n00b
“contains” makes more sense especially when you consider ranges with decimal endpoints or non-numeric ranges. The requirement to construct a range is that objects can be compared using <=> ie that the object can be ordered in some fashion. “covers” to me seems to be the inverse of “contains”. Go with “contains”.
Danno
I don’t know about the first, but I think a good name for the second method would be :among_successors_up_to? or maybe just :among_successors? if you’re willing to deal with a little ambiguity about the second argument.
murphy
Yes, something like
among?
is a very good idea.Kreiger
The thread says that a range does not “contain” elements, it “produces” elements.
(See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/167200 )
I sort of disagree, as i think the range implicitly defines a set, which definitely can “contain” elements.
However, if a range “produces” elements, then go with “produces?”.
Adam
(catching up on my reading after vacation)
The first problem to solve before fixing the names is the implementation of Range:
Right now, Ranges can be made of any objects that support <=> and succ. But a<=>a.succ != -1 for all a, especially if a is a String. So the first thing to do is change the requirement such that ‘rangeable’ objects must support ‘follows?’ and ‘succ’, where a.succ.follows?(a) == 1 and a.follows?(a.succ) == -1 for all a.
the follows method is equivalent to <=> for numeric types, but for strings it needs to be customized to match the succession generator.
Now that you have a sane range definition, you can have two names for range inclusion tests: range.spans(x) means range.first <= x <= range.last and
range.member?(x) means range.to_a.member?(x) (#because range.to_a uses succ to generate members. But it will use follows to skip the actual generation)
With this definition you would have:
('a'..'bb').member? 'z' #=> true ('a'..'bb').spans? 'z' #=> false ('a'..'bb').member? 'aardvark' #=> false ('a'..'bb').spans? 'aardvark' #=> true
Comments are closed for this entry.