O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#22 Using Procs for Compounded Filtering (matching_compound_members.rb)

In this script, we’ll filter Arrays using an arbitrary number of Procs. As before, we’ll open up the Array class, this time adding two methods. Again, we’ll filter digits based on simple mathematical tests. Let’s take a look at the source code and see what’s different.

The Code

  #!/usr/bin/env ruby
  # matching_compound_members.rb

  =begin rdoc
  Extend the built-in <b>Array</b> class.
  =end
  class Array

  =begin rdoc
  Takes a block as an argument and returns a list of
  members matching the criteria defined by that block.
  =end
❶   def matching_members(&some_block)    Block Arguments
      find_all(&some_block)
    end

  =begin rdoc
  Takes an <b>Array</b> of <b>Proc</b>s as an argument and returns all members ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required