[Ruby] find_all

Dmitry V. Sabanin sdmitry at lrn.ru
Wed Aug 20 13:45:02 MSD 2003


On Wednesday 20 August 2003 14:32, SoNaR wrote:
> Hello Ruby,
>
>   J`j find_all bngbp`y`er qeae pegsk|r`r sqknbmncn noep`rnp` b aknje?
Nwem| opnqrn:
class MyArr
  def initialize
    @arr = []
  end
  def concat arg
    @arr.concat arg
  end
  def find_all
    block_given? or return nil
    find_res = []
    @arr.each do |element|
      find_res << element if yield element
    end
    find_res
  end
end

test = MyArr.new
test.concat [0,1,2,3,4,5,6,7,8,9,10]
p test.find_all { |i| i < 5 } # B{bnd:  [0, 1, 2, 3, 4]

-- 
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.




More information about the Ruby mailing list