https://github.com/mdesantis/proc_for_case_equality
proc_for_case_equality - Ruby gem for using procs in case comparisons
https://github.com/mdesantis/proc_for_case_equality
Last synced: 5 months ago
JSON representation
proc_for_case_equality - Ruby gem for using procs in case comparisons
- Host: GitHub
- URL: https://github.com/mdesantis/proc_for_case_equality
- Owner: mdesantis
- License: mit
- Created: 2012-01-20T11:21:45.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2012-01-20T16:17:06.000Z (over 14 years ago)
- Last Synced: 2026-01-13T06:40:38.115Z (5 months ago)
- Language: Ruby
- Homepage: https://github.com/mdesantis/proc_for_case_equality
- Size: 148 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
= 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 === method, so I wrote a +ProcForCaseEquality+ class
that inherits from +Proc+ and overrides ===, 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
gem install proc_for_case_equality
== 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 = ProcForCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero? } }
any_multiple_of_3 = PFCE.new { |numbers| numbers.any? { |number| number.modulo(3).zero? } } # PFCE is provided by 'proc_for_case_equality/pfce'
# 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])