From: Victor Deryagin Date: 2011-05-28T15:20:21+09:00 Subject: Re: Problem in using %:whatever in enumumerations RichardOnRails writes: > 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: & 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.