* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / golf_prelude.rb
bloba13d3f71bc33ce29402df142e5449c3ca951cfaa
1 class Object
2   @@golf_hash = {}
4   verbose, $VERBOSE = $VERBOSE, nil
5   def method_missing m, *a, &b
6     t = @@golf_hash[ [m, self.class] ] ||= matching_methods(m)[0]
7     if t && b
8       __send__(t, *a) {|*args|
9         b.binding.eval("proc{|golf_matchdata| $~ = golf_matchdata }").call($~) if $~
10         b.call(*args)
11       }
12     else
13       t ? __send__(t, *a, &b) : super
14     end
15   end
16   $VERBOSE = verbose
18   def matching_methods(s = '', m = callable_methods)
19     r = /^#{s.to_s.gsub(/./){"(.*?)" + Regexp.escape($&)}}/
20     m.grep(r).sort_by do |i|
21       i.to_s.match(r).captures.map(&:size) << i
22     end
23   end
25   def self.const_missing c
26     t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c, constants)[0]
27     t and return const_get(t)
28     raise NameError, "uninitialized constant #{c}", caller(1)
29   end
31   def shortest_abbreviation(s = '', m = callable_methods)
32     s = s.to_s
33     our_case = (?A..?Z) === s[0]
34     if m.index(s.to_sym)
35       1.upto(s.size){|z| s.scan(/./).combination(z).map{|trial|
36         next unless ((?A..?Z) === trial[0]) == our_case
37         trial *= ''
38         return trial if matching_methods(trial, m)[0].to_s == s
39       }}
40     else
41       nil
42     end
43   end
45   def callable_methods
46     self.class == Object ? methods + private_methods : methods
47   end
49   private
51   def h(a = 'H', b = 'w', c = '!')
52     puts "#{a}ello, #{b}orld#{c}"
53   end
55   def f(m = 100)
56     1.upto(m){|n|puts'FizzBuzz
57 '[i=n**4%-15,i+13]||n}
58   end
60   alias say puts
62   def do_while
63     0 while yield
64   end
66   def do_until
67     0 until yield
68   end
69 end
71 class Array
72   alias old_to_s to_s
73   alias to_s join
74 end
76 class FalseClass
77   alias old_to_s to_s
78   def to_s
79     ""
80   end
81 end
83 class Integer
84   alias each times
85   include Enumerable
86 end
88 class String
89   alias / split
91   def to_a
92     split('')
93   end
95   (Array.instance_methods - instance_methods - %i[to_ary transpose flatten flatten! compact compact! assoc rassoc]).each{|meth|
96     eval "
97     def #{meth}(*args, &block)
98       a = to_a
99       result = a.#{meth}(*args, &block)
100       replace(a.join)
101       if result.class == Array
102         Integer === result[0] ? result.pack('c*') : result.join
103       elsif result.class == Enumerator
104         result.map(&:join).to_enum
105       else
106         result
107       end
108     end"
109   }
112 class Enumerator
113   alias old_to_s to_s
114   (Array.instance_methods - instance_methods - [:replace] + [:to_s]).each{|meth|
115     eval "
116     def #{meth}(*args, &block)
117       to_a.#{meth}(*args, &block)
118     end"
119   }
120   alias old_inspect inspect
121   alias inspect old_to_s
124 class Symbol
125   def call(*args, &block)
126     proc do |recv|
127       recv.__send__(self, *args, &block)
128     end
129   end