M
mgarriss
I have been using a little extension of Array that I wrote for awhile:
class Array
def and?() each{|e| return false unless yield e }; true end
def or?() each{|e| return true if yield e }; false end
end
etc...
Then I discovered Array#detect because of a post by Jason Creighton, he
used it in his WrapHash class (btw, nice class Jason). So I run to irb
and played with #detect. It's cool and I'll be using it but it's not a
replacement for my #and? and #or? methods. I still have to mess with
Array myself.
I looked for docs on Array#detect on http://www.rubycentral.com/ref/ and
I even looked in array.c (that was a big move for me). No luck. My
question is this: Where are these "extra" array methods documented? And
is there something already in Array that does what my #and? and #or?
methods do?
Michael Garriss
class Array
def and?() each{|e| return false unless yield e }; true end
def or?() each{|e| return true if yield e }; false end
end
=> false[1,2,3].and?{|e| e>0} => true
[1,2,3].and?{|e| e>1} => false
[1,2,3].or?{|e| e>1} => true
['this','that'].and?{|e|e.size==4} => true
['this','that'].and?{|e|e=~/th/} => true
['this','that'].and?{|e|e=~/at/} => false
['this','that'].or?{|e|e=~/at/} => true
[proc{|a|a.size==4},proc{|a|a=~/th/}].and?{|e|e.call("this")} => true
[proc{|a|a.size==4},proc{|a|a=~/th/}].and?{|e|e.call("these")}
etc...
Then I discovered Array#detect because of a post by Jason Creighton, he
used it in his WrapHash class (btw, nice class Jason). So I run to irb
and played with #detect. It's cool and I'll be using it but it's not a
replacement for my #and? and #or? methods. I still have to mess with
Array myself.
I looked for docs on Array#detect on http://www.rubycentral.com/ref/ and
I even looked in array.c (that was a big move for me). No luck. My
question is this: Where are these "extra" array methods documented? And
is there something already in Array that does what my #and? and #or?
methods do?
Michael Garriss