2007-12-25 10:04:30 +03:00
|
|
|
class Object
|
2007-12-30 17:20:37 +03:00
|
|
|
@@golf_hash = {}
|
2013-11-07 20:03:48 +04:00
|
|
|
|
2007-12-25 10:04:30 +03:00
|
|
|
def method_missing m, *a, &b
|
2013-11-07 20:03:48 +04: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
|
|
|
|
2013-11-07 20:03:48 +04:00
|
|
|
def matching_methods(s = '', m = callable_methods)
|
|
|
|
r = /^#{s.to_s.gsub(/./){"(.*?)" + Regexp.escape($&)}}/
|
2008-01-14 16:35:51 +03:00
|
|
|
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
|
2013-11-07 20:03:48 +04: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
|
|
|
|
|
2013-11-07 20:03:48 +04:00
|
|
|
def shortest_abbreviation(s = '', m = callable_methods)
|
|
|
|
s = s.to_s
|
|
|
|
our_case = (?A..?Z) === s[0]
|
2008-01-14 16:35:51 +03:00
|
|
|
if m.index(s.to_sym)
|
2013-11-07 20:03:48 +04:00
|
|
|
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
|
2008-01-14 16:35:51 +03:00
|
|
|
}}
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def callable_methods
|
|
|
|
self.class == Object ? methods + private_methods : methods
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-11-07 20:03:48 +04:00
|
|
|
def h(a = 'H', b = 'w', c = '!')
|
2007-12-25 10:42:53 +03:00
|
|
|
puts "#{a}ello, #{b}orld#{c}"
|
|
|
|
end
|
2007-12-28 05:18:05 +03:00
|
|
|
|
2018-05-21 09:20:24 +03:00
|
|
|
def f(m = 100)
|
|
|
|
1.upto(m){|n|puts'FizzBuzz
|
|
|
|
'[i=n**4%-15,i+13]||n}
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2017-09-07 15:31:50 +03:00
|
|
|
(Array.instance_methods - instance_methods - %i[to_ary transpose flatten flatten! compact compact! assoc rassoc]).each{|meth|
|
2013-11-07 20:03:48 +04:00
|
|
|
eval "
|
2008-09-03 16:21:32 +04:00
|
|
|
def #{meth}(*args, &block)
|
2013-11-07 20:03:48 +04:00
|
|
|
a = to_a
|
2008-01-14 16:35:51 +03:00
|
|
|
result = a.#{meth}(*args, &block)
|
|
|
|
replace(a.join)
|
|
|
|
if result.class == Array
|
2013-11-07 20:03:48 +04:00
|
|
|
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
|
2013-11-07 20:03:48 +04:00
|
|
|
(Array.instance_methods - instance_methods - [:replace] + [:to_s]).each{|meth|
|
|
|
|
eval "
|
2008-09-03 16:21:32 +04:00
|
|
|
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
|