No commit activity in last 3 years
No release in over 3 years
Single clause, branching method definitions are so 2007. Get with SRP and party! With overloaded_methods, you can separate out any conditional branching in your method to where it belongs: predicates! Feel the power of the chain-of-responsibility pattern in your own code!!!!!!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 3.0
 Project Readme

Overloaded Methods

Build Status Gem Version Code Climate Test Coverage

http://github.com/coreyhaines/overloaded_methods

Description

overloaded_methods was inspired by erlang pattern matching. Basically, the idea is that you can specify a method and several code blocks based on aspects of the parameters. Here's an example:

overload_method :even_odd do |m|
  m.when {|number| number % 2 == 0 }.do { :even }
  m.when {|number| number % 2 == 1 }.do { :odd }
end

You can then call even_odd(2) and get :even, even_odd(3) gives you :odd.

the specs have more examples.

Special thanks to Jim Weirich for suggesting some great improvements in the syntax.

There is an alternate syntax with pattern/does

overload_method :even_odd do |m|
  m.pattern {|number| number % 2 == 0 }.does { :even }
  m.pattern {|number| number % 2 == 1 }.does { :odd }
end

However, this syntax may or may not be deprecated in favor of the when/do syntax.

Here's an example of Fibonacci calculation:

class CalculatesFibonacci

  class << self
    include OverloadedMethods
    overload_method :entry do |m|
      m.when{|which| which <= 1}.do{|which| which}
      m.default{|which|
          CalculatesFibonacci.entry(which-1) + CalculatesFibonacci.entry(which-2)
        }
    end
  end
end

Synopsis

Here are some examples of how to use it from the specs.

class WithWhenDo
  include OverloadedMethods

  overload_method :even_odd do |m|
    m.when {|number| number % 2 == 0 }.do { :even }
    m.when {|number| number % 2 == 1 }.do { :odd }
  end

  overload_method :hello_world do |m|
    m.when { true }.do {'hello, world'}
  end

  overload_method :return_params do |m|
    m.when { |param1, param2| true}.do {|param1, param2| [param1, param2]}
  end

  overload_method :two_params_collected do |m|
    m.default { |param1, *params| [param1, params] }
  end

  overload_method :number_of_digits do |m|
    m.when {|number| number < 10}.do {1}
    m.when {|number| number< 100}.do {2}
    m.default { 'more than two digits is crazy talk'}
  end

  overload_method :default_returns_parameters do |m|
    m.default { |*params| params}
  end

end

Requirements

Ruby

Install

gem install overloaded_methods

or, if using Bundler,

gem 'overloaded_methods'

Do you use this

Please let me know

License

See License.txt

Monads

Monads