Sha256: 64fb00ae7a1046a730bde5c54a97264607ce0ef41fd986f06ecd7ca6d431dd3d

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

= ProcForCaseEquality.new { puts 'procs in case statements are cool!' }

== Features

Simple yet powerful: it lets you use procs for case comparisons (see the example below).

== How

+case+ statements call the <tt>===</tt> method, so I wrote a +ProcForCaseEquality+ class
that inherits from +Proc+ and overrides <tt>===</tt>, letting the case statement call the proc
passing the value of the case as argument.

The source code is so simple that I can put it in full here:

    class ProcForCaseEquality < Proc
      def ===(*params)
        self.call *params
      end
    end

5 LOCs :P

== Installation

<tt>gem install proc_for_case_equality</tt> *COMING*

== Usage / Examples

    require 'proc_for_case_equality' # OR:
    require 'proc_for_case_equality/pfce' # if you want PFCE constant to point to ProcForCaseEquality

    # Define some procs
    all_multiples_of_3 = ProcWithCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero? } }
    any_multiple_of_3  = ProcWithCaseEquality.new { |numbers| numbers.any? { |number| number.modulo(3).zero? } }

    # Here we come
    case [1, 2, 3]
    when all_multiples_of_3 
      puts 'all numbers are multiples of 3'
    when any_multiple_of_3
      puts 'at least one number is multiple of 3'
    else
      puts 'no multiples of 3'
    end

== Inspired by

{This article}[http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/]

== License

MIT (see {LICENSE}[https://github.com/ProGNOMmers/proc_for_case_equality/blob/master/LICENSE])

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proc_for_case_equality-0.1 README.rdoc