hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Two new method names relevant to Range #

by babie in inspect

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]

said on 20 Dec 2005 at 12:25

Obviously, the first should be “contains?”, as that’s the word you use.

The other might be “in?”

said on 20 Dec 2005 at 13:11

Indeed, “contains” seems the most logical choice for me too.

said on 20 Dec 2005 at 13:15

Wait, include? is going away for numbers?

Console me in my sorrow!

said on 20 Dec 2005 at 13:22

if x in beg..end

said on 20 Dec 2005 at 13:27

Scratch that—I read the thread and I can see why include? is ambiguous.

said on 20 Dec 2005 at 13:29

I like (beg..end).has?(x), which I see has already been suggested even though I can’t read the rest.

said on 20 Dec 2005 at 13:34

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.

said on 20 Dec 2005 at 14:23

Maybe this doesn’t fit too well with other stuff, but it kind of makes sense to me …

(example with 1.8)

irb(main):001:0> class Range
irb(main):002:1> alias :=~ :include?
irb(main):003:1> end
=> nil
irb(main):004:0> (3..10) =~ 5
=> true
irb(main):005:0> (3..10) =~ 50
=> false
said on 20 Dec 2005 at 14:56

(beg..end).bounds?(x) x.bounded_by?(beg..end)

said on 20 Dec 2005 at 15:15

(beg..end).covers?(x)

said on 20 Dec 2005 at 17:11

I’d call the first range.enclose?(item).

I’m not sure about the second—what’s wrong about .include?()?

said on 20 Dec 2005 at 17:23

Thank you for translating!

Remeber that method names should not be conjugated – so contain without s.

What about this:

('a'..'aa').wrap? 'z'  # or
 ('a'..'aa').surround? 'z'

“Surround” is one of my favorite English words :)

said on 20 Dec 2005 at 17:47

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:

(1..7).include? 4  #-> true
 (1..7).member? 4 #-> true, synonym
 4.between?(1, 7)  #-> true

But with strings, it’s now:

('a'..'aa').to_a  #-> 'a', 'b', ..., 'z', 'aa'
 ('a'..'aa').include? 'b'  #-> true (new!)
 ('a'..'aa').member? 'b'  #-> true, synonym
'b'.between?('a', 'aa')  #-> false, as before

And now we need two methods that do it the other way: a comparison include? and a discreete between?:

('a'..'aa').cover? 'b'  #-> false
 ('a'..'aa').surround? 'b'  #-> false, alternative
 ('a'..'aa').wrap? 'b'  #-> false, alternative
 ('a'..'aa').bound? 'b'  #-> false, alternative
'b'.found_between?('a', 'aa')  #-> true
 'b'.in?('a', 'aa')  #-> true

I hope this clarifies my and your understanding of this advanced math philosophic stuff. Names are important.

said on 20 Dec 2005 at 19:42

“within range” is quite appropriate. However (beg..end).within? sounds the other way.. unless it acn be x.within? Range

said on 20 Dec 2005 at 21:51

phil602 sorry, my translation is wrong.

In 1.9, “include?” checks Number by magnitude relation.

said on 20 Dec 2005 at 22:14

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?”.

said on 21 Dec 2005 at 00:07

If member?/include? go back to checking a #succ “hit” then

r = 0..10000000000000000000000000000000000
r.member? 10000000000000000000000000000000

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

said on 21 Dec 2005 at 07:44

“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”.

said on 21 Dec 2005 at 08:04

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.

said on 22 Dec 2005 at 07:07

Yes, something like among? is a very good idea.

said on 22 Dec 2005 at 10:27

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?”.

said on 09 Jan 2006 at 15:49

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