--k+w/mQv8wyuph6w0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Apr 27, 2011 at 05:52:17PM +0900, Jes Gabriel y Gal wrote:
> 
> Generalizing a bit more:
> 
> lower_major, lower_minor = "3:9".split(":").map {|x| x.to_i}
> upper_major, upper_minor = "3:31".split(":").map {|x| x.to_i}
> major_range = lower_major..upper_major
> minor_range = lower_minor..upper_minor
> 
> lines.each do |line|
>   if line =~ /^(\d+):(\d+)/
>    major, minor = $1.to_i, $2.to_i
>    puts line if major_range.include?(major) and minor_range.include?(minor)
>  end
> end

This might be a bit overkill, but as an extention of that general idea:

  class String
    def parse_labels
      self.split(':').collect {|n| n.to_i }
    end
  end
  
  class Array
    def inferior_range(records)
      records.each do |r|
        yield r if Range.new(*self).include? r.parse_labels[1]
      end
    end
  end

  foo = [
    '1:1 foo',
    '1:2 bar',
    '1:3 baz',
    '1:4 qux',
    '1:5 fee',
    '1:6 fie',
    '1:7 foe',
    '1:8 fum',
  ]

  [2,5].inferior_range(foo) {|rec| puts rec }

. . . and the output of that should be:

  1:2 bar
  1:3 baz
  1:4 qux
  1:5 fee

Return value would be:

  => ["1:1 foo", "1:2 bar", "1:3 baz", "1:4 qux", "1:5 fee", "1:6 fie",
  "1:7 foe", "1:8 fum"]

It's a somewhat naive implementation of the idea -- doing a little ugly
monkeypatching, failing to validate any data, tightly coupling the Array
method with the String method, and so on.  Maybe using modules to compose
this stuff would be better, or subclassing somewhere along the way.  My
only defense is "This was a fun diversion for a few moments."

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

--k+w/mQv8wyuph6w0
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk24UGgACgkQ9mn/Pj01uKWe2gCg1t8O+W0SvhmYLt+uCv6at2l2
cMIAmQETP2ECPCLwG4rmVb/d4MjhsbP4
pO
-----END PGP SIGNATURE-----

--k+w/mQv8wyuph6w0--