ruby picture

RCR 152: all() and any() like in Perl6

Submitted by anonymous (Thu Aug 21 14:08:13 UTC 2003)

Abstract

This is a legacy RCR from Ruby Garden, submitted by anonymous. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

I'd like Ruby to have built in 'all' and 'any' methods like those planned for Perl6.
These allow you things like:

    puts "something" if any(2,5,8) == 3<br>
or:
    if all(x,y,z) 

A possible implementation like this should go to Kernel:

class All
    def initialize(ary)<br>
        @ary = ary<br>
    end<br>

    def ==(*ary)<br>
        @ary.each{|i|<br>
            ary.each{|j|<br>

return false unless i == j

            }<br>
        }<br>
        return true<br>
    end<br>

    def 
        @ary.each{|i|<br>
            ary.each{|j|<br>

return false if i&gt; j

            }<br>
        }<br>
        return true<br>
    end<br>

    def&amp;gt;(*ary)<br>
        @ary.each{|i|<br>
            ary.each{|j|<br>

return false if i

            }<br>
        }<br>
        return true<br>
    end<br>
end
def all(*ary)
    return All.new(ary)<br>
end

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting

Ruby-Junction does this (cout, 2003-08-21 14:21:02)

quantum operations? (, 2003-08-21 15:34:25)

Weren't these added to Perl6 to make it easy to model quantum logic?

Ruby 1.8: Enumerable#any? and Enumerable#all? (whytheluckystif, 2003-08-21 20:30:22)

These are in 1.8.0.


            
  puts "something" if [2,5,8].any? { |x| x == 3 }

            

or:


            
  if [x,y,z].all?

            

So, if you leave off a block, it defaults to checking of the elements of the Enumerable evaluate as booleans to true.

See my >1.8.0 summary page for more on these.


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .