2007-12-25 10:04:30 +03:00
|
|
|
class Object
|
2007-12-30 17:20:37 +03:00
|
|
|
@@golf_hash = {}
|
2007-12-25 10:04:30 +03:00
|
|
|
def method_missing m, *a, &b
|
2008-01-14 16:35:51 +03:00
|
|
|
t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]
|
2009-03-17 09:40:20 +03:00
|
|
|
if t && b
|
|
|
|
__send__(t, *a) {|*args|
|
|
|
|
b.binding.eval("proc{|golf_matchdata| $~ = golf_matchdata }").call($~) if $~
|
|
|
|
b.call(*args)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
t ? __send__(t, *a, &b) : super
|
|
|
|
end
|
2007-12-25 10:04:30 +03:00
|
|
|
end
|
2007-12-25 10:42:53 +03:00
|
|
|
|
2008-01-14 16:35:51 +03:00
|
|
|
def matching_methods(s='', m=callable_methods)
|
|
|
|
r=/^#{s.to_s.gsub(/./){"(.*?)"+Regexp.escape($&)}}/
|
|
|
|
m.grep(r).sort_by do |i|
|
2009-03-17 09:40:20 +03:00
|
|
|
i.to_s.match(r).captures.map(&:size) << i
|
2008-01-14 16:35:51 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-28 04:07:21 +03:00
|
|
|
def self.const_missing c
|
2008-03-17 10:35:11 +03:00
|
|
|
t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c,constants)[0]
|
2008-01-14 16:35:51 +03:00
|
|
|
t and return const_get(t)
|
2007-12-28 07:46:56 +03:00
|
|
|
raise NameError, "uninitialized constant #{c}", caller(1)
|
2007-12-28 04:07:21 +03:00
|
|
|
end
|
|
|
|
|
2008-01-14 16:35:51 +03: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 10:42:53 +03:00
|
|
|
def h(a='H', b='w', c='!')
|
|
|
|
puts "#{a}ello, #{b}orld#{c}"
|
|
|
|
end
|
2007-12-28 05:18:05 +03:00
|
|
|
|
2007-12-28 14:20:59 +03:00
|
|
|
alias say puts
|
2008-01-14 16:35:51 +03:00
|
|
|
|
|
|
|
def do_while
|
|
|
|
0 while yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_until
|
|
|
|
0 until yield
|
|
|
|
end
|
2007-12-25 10:04:30 +03:00
|
|
|
end
|
2007-12-27 19:47:58 +03:00
|
|
|
|
2008-01-09 12:28:19 +03:00
|
|
|
class Array
|
2008-01-14 16:35:51 +03:00
|
|
|
alias old_to_s to_s
|
2008-01-09 12:28:19 +03:00
|
|
|
alias to_s join
|
|
|
|
end
|
|
|
|
|
|
|
|
class FalseClass
|
2008-01-14 16:35:51 +03:00
|
|
|
alias old_to_s to_s
|
2008-01-09 12:28:19 +03:00
|
|
|
def to_s
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-27 19:47:58 +03:00
|
|
|
class Integer
|
2007-12-28 07:59:00 +03:00
|
|
|
alias each times
|
2007-12-27 19:47:58 +03:00
|
|
|
include Enumerable
|
|
|
|
end
|
2007-12-30 17:49:22 +03:00
|
|
|
|
|
|
|
class String
|
|
|
|
alias / split
|
2008-01-14 16:35:51 +03:00
|
|
|
|
|
|
|
def to_a
|
|
|
|
split('')
|
|
|
|
end
|
|
|
|
|
|
|
|
(Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth|
|
2008-09-03 16:21:32 +04:00
|
|
|
eval"
|
|
|
|
def #{meth}(*args, &block)
|
2008-01-14 16:35:51 +03:00
|
|
|
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 12:03:30 +04:00
|
|
|
elsif result.class == Enumerator
|
2008-01-14 16:35:51 +03:00
|
|
|
result.map(&:join).to_enum
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-08-13 12:03:30 +04:00
|
|
|
class Enumerator
|
2008-01-14 16:35:51 +03:00
|
|
|
alias old_to_s to_s
|
|
|
|
(Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth|
|
2008-09-03 16:21:32 +04:00
|
|
|
eval"
|
|
|
|
def #{meth}(*args, &block)
|
2008-01-14 16:35:51 +03:00
|
|
|
to_a.#{meth}(*args, &block)
|
|
|
|
end"
|
|
|
|
}
|
2009-01-23 11:26:10 +03:00
|
|
|
alias old_inspect inspect
|
2008-01-14 16:35:51 +03:00
|
|
|
alias inspect old_to_s
|
2007-12-30 17:49:22 +03:00
|
|
|
end
|
2011-03-07 11:44:45 +03:00
|
|
|
|
|
|
|
class Symbol
|
|
|
|
def call(*args, &block)
|
|
|
|
proc do |recv|
|
|
|
|
recv.__send__(self, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|