Silly Code from The Muppet Laboratories

R

Robert Klemme

.... 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}
 
J

James Edward Gray II

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

This was a Ruby Quiz just the week before last:

http://www.rubyquiz.com/quiz110.html

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top