Class: Module

Inherits:
Object show all
Defined in:
lib/call-me/named.rb,
lib/call-me/memoize.rb,
lib/call-me/overload.rb,
lib/call-me/pattern-matching.rb

Instance Method Summary collapse

Instance Method Details

#def_signature(*sign) ⇒ Object



47
48
49
# File 'lib/call-me/overload.rb', line 47

def def_signature (*sign)
	@__to_overload__ = sign
end

#define_overloadable(name, default = nil, matchers) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/call-me/overload.rb', line 51

def define_overloadable (name, default = nil, matchers)
	define_method name do |*args|
		matchers.each {|signature, body|
			return instance_exec *args, &body if (signature.is_a?(Array) ? signature : [signature]).each_with_index.all? {|klass, index|
				args[index].is_a?(klass)
			}
		}

		return instance_exec *args, &default if default

		raise ArgumentError, "the arguments don't match any signature"
	end
end

#define_pattern_matched(name, default = nil, matchers) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/call-me/pattern-matching.rb', line 63

def define_pattern_matched (name, default = nil, matchers)
	define_method name do |*args|
		matchers.each {|signature, body|
			return instance_exec *args, &body if (if signature.is_a?(Proc)
				signature.call(*args)
			else
				(signature.is_a?(Array) ? signature : [signature]) == args
			end)
		}

		return instance_exec *args, &default if default

		raise ArgumentError, "non-exhaustive patterns"
	end
end

#is_overloaded?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/call-me/overload.rb', line 43

def is_overloaded? (name)
	__overloaded__.has_key?(name)
end