Module: Matchrb

Extended by:
Matchrb
Included in:
Matchrb
Defined in:
lib/matchrb.rb,
lib/matchrb/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#match(something, patterns) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/matchrb.rb', line 6

def match(something, patterns)
  apply = lambda do |action, value|
    return action unless action.respond_to?(:to_proc)

    action = action.to_proc
    action.arity.zero? ? action[] : action[value]
  end

  patterns.each do |pattern, action|
    if (value = pattern === something)
      return apply[action, value]
    end
  end

  nil
end

#otherwiseObject



23
24
25
# File 'lib/matchrb.rb', line 23

def otherwise
  Object
end