# More readable array querying

**URL:** <https://www.ruby-forum.com/t/more-readable-array-querying/196288>\
**Category:** Ruby\
**Created:** [October 10, 2010, 11:00pm UTC](https://www.ruby-forum.com/t/more-readable-array-querying/196288 "2010-10-10T23:00:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [October 10, 2010, 11:00pm UTC](https://www.ruby-forum.com/t/more-readable-array-querying/196288/1 "2010-10-10T23:00:24Z")

</div>

All,  
I’ve been playing with some code to make querying an array more  
readable.  
Basically, it lets you write one of the following:

puts [1,2,3].first that {|x| x.even? }  
puts [1,2,3].first that is :even

instead of this:

puts [1,2,3].find {|x| x.even? }

The code is below. I’m curious whether you’d find it useful,  
interesting,  
worthwhile, etc., and if there are improvements you can think of. Either  
way, it was a fun little exercise. I love the flexibility I get from  
writing  
in ruby!

module Kernel  
def that proc=nil, &block  
p = proc.nil? ? block : proc  
lambda { |x| x.instance\_eval &p }  
end

def is message  
lambda { |x| x.send “#{message}?”.to\_sym }  
end  
end

class Array  
alias :old\_first first

def first func=nil  
return old\_first if func.nil?  
each do |x|  
return x if func.call x  
end  
nil  
end  
end

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [October 11, 2010, 12:33am UTC](https://www.ruby-forum.com/t/more-readable-array-querying/196288/2 "2010-10-11T00:33:28Z")

</div>

On Oct 10, 2010, at 13:59 , Andrew W. wrote:

> The code is below. I’m curious whether you’d find it useful, interesting,  
> worthwhile, etc., and if there are improvements you can think of. Either  
> way, it was a fun little exercise. I love the flexibility I get from writing  
> in ruby!

I doubt I would ever want to use this. The amount of readability it adds  
is completely offset by how much slower it is:

# of iterations = 1000000

```
                      user system total real

```

null\_time 0.090000 0.000000 0.090000 ( 0.095464)  
ary.find 1.680000 0.010000 1.690000 ( 1.717772)  
ary.first that 18.580000 1.140000 19.720000 ( 23.080667)  
ary.first that is 21.770000 0.260000 22.030000 ( 30.326090)

> def that proc=nil, &block  
> p = proc.nil? ? block : proc

p = proc || block

> def is message  
> lambda { |x| x.send “#{message}?”.to\_sym }

no need for to\_sym at all.

> def first func=nil  
> return old\_first if func.nil?  
> each do |x|  
> return x if func.call x  
> end

Any reason why you’re not using find?

Also, if you don’t match, you’re going to return self, which would be  
wrong imo.

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [October 11, 2010, 2:05am UTC](https://www.ruby-forum.com/t/more-readable-array-querying/196288/3 "2010-10-11T02:05:21Z")

</div>

Thanks for the helpful feedback.

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [October 11, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/more-readable-array-querying/196288/4 "2010-10-11T12:05:43Z")

</div>

On Oct 10, 3:59 pm, Andrew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> lambda { |x| x.instance_eval &p }
> 
> ```
> 
> def first func=nil  
> return old\_first if func.nil?  
> each do |x|  
> return x if func.call x  
> end  
> nil  
> end  
> end

Which is more readable?

2 + 2 = 4  
Two added to two is equal to four.

And if you’re interested in readability, why do you say  
lambda instead of proc?
