Robert Klemme
2/2/2007 6:59:00 PM
.... for lazy typers - can also be used for obfuscation. Might make your
code unmaintainable, instable or both.
Use at own risk.
robert
module Kernel
alias _method_missing method_missing
def method_missing(s,*a,&b)
candidates = method_candidates(s)
case candidates.size
when 0
_method_missing(s,*a,&b)
when 1
send(candidates[0],*a,&b)
else
raise NameError,
"Ambiguous name: #{s}, " "candidates: #{candidates.join ', '}"
end
end
private
# find names of which sym is prefix
def method_candidates(sym)
methods.grep(/\A#{sym}/i)
end
end
p %w{foo bar}.inj(0) {|le, s| le + s.length}
module Kernel
# get rid of aeiou and special chars
def method_candidates(sym)
methods.select {|me| /\A#{sym.to_s.gsub(/[aeiou_]/, '')}/i =~
me.gsub(/[aeiou_]/, '')}
end
end
p %w{foo bar}.injct(0) {|le, s| le + s.length}
p %w{foo bar}.inj(0) {|le, s| le + s.length}
p %w{foo bar}.in(0) {|le, s| le + s.length}