module Ov::Ext

Public Instance Methods

match(*args, &block) click to toggle source

Add `match` method, which work like `case` statement but for types

Usage

include Ov::Ext    

match("String", "dsa") do 
  try(String, Array) {|str, arr| "#{str} #{arr}" }
  try(String) {|str| "#{str}"  }
  otherwise { "none" }
end
# File lib/ov/ext/matching.rb, line 17
def match(*args, &block)
  z = Module.new do 
    include Ov
    extend self
    def try(*args, &block)
      let :anon_method, *args, &block
    end
    def otherwise(&block)
      let :otherwise, &block
    end
    instance_eval &block
  end
  begin
    z.anon_method(*args)
  rescue Ov::NotImplementError => e 
    z.otherwise
  end  
end
otherwise(&block) click to toggle source
# File lib/ov/ext/matching.rb, line 24
def otherwise(&block)
  let :otherwise, &block
end
try(*args, &block) click to toggle source
# File lib/ov/ext/matching.rb, line 21
def try(*args, &block)
  let :anon_method, *args, &block
end