[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Silly Code from The Muppet Laboratories

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}

3 Answers

James Gray

2/2/2007 7:08:00 PM

0

On Feb 2, 2007, at 1:00 PM, Robert Klemme wrote:

> 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/qu...

James Edward Gray II

Robert Klemme

2/2/2007 7:11:00 PM

0

On 02.02.2007 20:08, James Edward Gray II wrote:
> This was a Ruby Quiz just the week before last:
>
> http://www.rubyquiz.com/qu...
>
> James Edward Gray II

Umpf! I better stop posting now in order to prevent more embarrassment...

robert

Gavin Kistner

2/2/2007 7:13:00 PM

0

On Feb 2, 12:08 pm, James Edward Gray II <j...@grayproductions.net>
wrote:
> This was a Ruby Quiz just the week before last:
> http://www.rubyquiz.com/qu...

Heh, who needs gems? Soon the answers to all problems will have been
solved as a quiz at some point. :)