2007-12-25 02:04:30 -05:00
|
|
|
class Object
|
2007-12-30 09:20:37 -05:00
|
|
|
@@golf_hash = {}
|
2007-12-25 02:04:30 -05:00
|
|
|
def method_missing m, *a, &b
|
2008-01-14 08:35:51 -05:00
|
|
|
t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]
|
2007-12-25 02:04:30 -05:00
|
|
|
t ? __send__(t, *a, &b) : super
|
|
|
|
end
|
2007-12-25 02:42:53 -05:00
|
|
|
|
2008-01-14 08:35:51 -05:00
|
|
|
def matching_methods(s='', m=callable_methods)
|
|
|
|
r=/^#{s.to_s.gsub(/./){"(.*?)"+Regexp.escape($&)}}/
|
|
|
|
m.grep(r).sort_by do |i|
|
|
|
|
i.to_s.match(r).captures.map(&:size)<<i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-27 20:07:21 -05:00
|
|
|
def self.const_missing c
|
2008-03-17 03:35:11 -04:00
|
|
|
t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c,constants)[0]
|
2008-01-14 08:35:51 -05:00
|
|
|
t and return const_get(t)
|
2007-12-27 23:46:56 -05:00
|
|
|
raise NameError, "uninitialized constant #{c}", caller(1)
|
2007-12-27 20:07:21 -05:00
|
|
|
end
|
|
|
|
|
2008-01-14 08:35:51 -05:00
|
|
|
def shortest_abbreviation(s='', m=callable_methods)
|
|
|
|
s=s.to_s
|
|
|
|
our_case = (?A..?Z)===s[0]
|
|
|
|
if m.index(s.to_sym)
|
|
|
|
1.upto(s.size){|z|s.scan(/./).combination(z).map{|trial|
|
|
|
|
next unless ((?A..?Z)===trial[0]) == our_case
|
|
|
|
trial*=''
|
|
|
|
return trial if matching_methods(trial,m)[0].to_s==s
|
|
|
|
}}
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def callable_methods
|
|
|
|
self.class == Object ? methods + private_methods : methods
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2007-12-25 02:42:53 -05:00
|
|
|
def h(a='H', b='w', c='!')
|
|
|
|
puts "#{a}ello, #{b}orld#{c}"
|
|
|
|
end
|
2007-12-27 21:18:05 -05:00
|
|
|
|
2007-12-28 06:20:59 -05:00
|
|
|
alias say puts
|
2008-01-14 08:35:51 -05:00
|
|
|
|
|
|
|
def do_while
|
|
|
|
0 while yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_until
|
|
|
|
0 until yield
|
|
|
|
end
|
2007-12-25 02:04:30 -05:00
|
|
|
end
|
2007-12-27 11:47:58 -05:00
|
|
|
|
2008-01-09 04:28:19 -05:00
|
|
|
class Array
|
2008-01-14 08:35:51 -05:00
|
|
|
alias old_to_s to_s
|
2008-01-09 04:28:19 -05:00
|
|
|
alias to_s join
|
|
|
|
end
|
|
|
|
|
|
|
|
class FalseClass
|
2008-01-14 08:35:51 -05:00
|
|
|
alias old_to_s to_s
|
2008-01-09 04:28:19 -05:00
|
|
|
def to_s
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-27 11:47:58 -05:00
|
|
|
class Integer
|
2007-12-27 23:59:00 -05:00
|
|
|
alias each times
|
2007-12-27 11:47:58 -05:00
|
|
|
include Enumerable
|
|
|
|
end
|
2007-12-30 09:49:22 -05:00
|
|
|
|
|
|
|
class String
|
|
|
|
alias / split
|
2008-01-14 08:35:51 -05:00
|
|
|
|
|
|
|
def to_a
|
|
|
|
split('')
|
|
|
|
end
|
|
|
|
|
|
|
|
(Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth|
|
|
|
|
eval"def #{meth}(*args, &block)
|
|
|
|
a=to_a
|
|
|
|
result = a.#{meth}(*args, &block)
|
|
|
|
replace(a.join)
|
|
|
|
if result.class == Array
|
|
|
|
Integer===result[0] ? result.pack('c*') : result.join
|
2008-08-13 04:03:30 -04:00
|
|
|
elsif result.class == Enumerator
|
2008-01-14 08:35:51 -05:00
|
|
|
result.map(&:join).to_enum
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-08-13 04:03:30 -04:00
|
|
|
class Enumerator
|
2008-01-14 08:35:51 -05:00
|
|
|
alias old_to_s to_s
|
|
|
|
(Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth|
|
|
|
|
eval"def #{meth}(*args, &block)
|
|
|
|
to_a.#{meth}(*args, &block)
|
|
|
|
end"
|
|
|
|
}
|
|
|
|
alias inspect old_to_s
|
2007-12-30 09:49:22 -05:00
|
|
|
end
|