Problem in using %:whatever in enumumerations

Hi,

I’m on a Ruby self-improvement mission. Presently I working my way
though all 47 of the Enumerable methods in Ruby 1.9.2. I composed the
following:

greek = %w{Alpha Beta Gamma}
puts greek.all? { |s| s.length == 5 } # (correct) => false
puts greek.all? { |s| (4…5).include? s.length } # (correct) => true
puts greek.all?(&:length) # (programmer error) => true

The last line is programmed incorrectly because I couldn’t stick in
the intended “5” in a syntactically correct way. Is the most
effective way construction of a lamda/proc? If so, how?

Thanks in advance,
Richard:

RichardOnRails [email protected] writes:

The last line is programmed incorrectly because I couldn’t stick in
the intended “5” in a syntactically correct way. Is the most
effective way construction of a lamda/proc? If so, how?

Thanks in advance,
Richard:

& converts method with a given name to Proc object, for example:

&:length

becomes:

{ |s| s.length }

If that’s not what you want (e.g. you need { |s| s.length == 5 }) -
construct block yourself. There is no way to specify arguments when
using &:length.

On May 28, 2:20am, Victor D. [email protected] wrote:

Thanks for your response.

greek = %w{Alpha Beta Gamma}
puts greek.all? { |s| s.length == 5 } # (correct) => false
puts greek.all? { |s| (4…5).include? s.length } # (correct) => true
puts greek.all?(&:length) # (programmer error) => true

… If that’s not what you want (e.g. you need { |s| s.length == 5 }) -
construct block yourself.

I did construct the block myself in the first couple of examples
(lines 2 & 3) but was searching (in line 4) for another way of
expressing the requirement.

I finally found a way to use a lambda in the following statements in
place of line 4:

lam = lambda { |item, len| item.length == 5 }
puts greek.all? { |s| lam.call(s,5)} # (correct) => false

Of course, the verbosity of this pair of statements is unsatisfying,
so I’m still searching for succinct alternatives.

Best wishes,
Richard

If you just intend to use a lambda, here is another syntactic sugar:

greek.all? &-> e { e.length == 5 }

But I would stick to the first approach.

On May 28, 3:08pm, Christopher D. [email protected] wrote:

module Enumerable
def all_have?(attribute, value)
all? { |item| item.send(attribute) == value }
end
end

greek.all_have? :length, 5

Hi Christopher,

Philosophically, that’s just the kind of thing I wanted (dynamic
application of criteria) but didn’t have the wit to describe it.

As you saw in my post, I had the statement:
puts greek.all? { |s| s.length == 5 }

But if I had a number of criteria and values to apply, my code would
get messy fast and invite copy & paste errors. Your code is tidy,
more readable and can be made as short as I wish without loss of
clarity.

Many thanks,
Richard

On Sat, May 28, 2011 at 7:00 AM, RichardOnRails
[email protected] wrote:

construct block yourself.

I did construct the block myself in the first couple of examples
(lines 2 & 3) but was searching (in line 4) for another way of
expressing the requirement.

I finally found a way to use a lambda in the following statements in
place of line 4:

lam = lambda { |item, len| item.length == 5 }
puts greek.all? { |s| lam.call(s,5)} # (correct) => false

Note that the second argument to lambda is never used, as you’ve
hardcoded 5 for the length to use for the test rather than using the
second argument. If you want something where you can change the length
to test for on different calls, replace the first line with:

lam = lambda { |item, len| item.length == len }

Of course, the verbosity of this pair of statements is unsatisfying,
so I’m still searching for succinct alternatives.

There isn’t anything I can think of built in that’s more succinct, but
if this kind of test is common in your code, you can abstract part of
it out to a library method on Enumerable and make the code more
succinct and clear at the point of call.

elsewhere, extend Enumerable

(obvious parallels any_have?, one_has?, count_having, etc. could

also be defined)

module Enumerable
def all_have?(attribute, value)
all? { |item| item.send(attribute) == value }
end
end

then, in the point you were working on

greek.all_have? :length, 5