Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (88 sloc) 2.163 kb
511dc44 initial import
Laurent Sansonetti authored
1 class Object
2 @@golf_hash = {}
3 def method_missing m, *a, &b
4 t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]
5 t ? __send__(t, *a, &b) : super
6 end
7
8 def matching_methods(s='', m=callable_methods)
9 r=/^#{s.to_s.gsub(/./){"(.*?)"+Regexp.escape($&)}}/
10 m.grep(r).sort_by do |i|
11 i.to_s.match(r).captures.map(&:size)<<i
12 end
13 end
14
15 def self.const_missing c
eee9d7b sync with ruby trunk r15665
Laurent Sansonetti authored
16 t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c,constants)[0]
511dc44 initial import
Laurent Sansonetti authored
17 t and return const_get(t)
18 raise NameError, "uninitialized constant #{c}", caller(1)
19 end
20
21 def shortest_abbreviation(s='', m=callable_methods)
22 s=s.to_s
23 our_case = (?A..?Z)===s[0]
24 if m.index(s.to_sym)
25 1.upto(s.size){|z|s.scan(/./).combination(z).map{|trial|
26 next unless ((?A..?Z)===trial[0]) == our_case
27 trial*=''
28 return trial if matching_methods(trial,m)[0].to_s==s
29 }}
30 else
31 nil
32 end
33 end
34
35 def callable_methods
36 self.class == Object ? methods + private_methods : methods
37 end
38
39 private
40
41 def h(a='H', b='w', c='!')
42 puts "#{a}ello, #{b}orld#{c}"
43 end
44
45 alias say puts
46
47 def do_while
48 0 while yield
49 end
50
51 def do_until
52 0 until yield
53 end
54 end
55
56 class Array
57 alias old_to_s to_s
58 alias to_s join
59 end
60
61 class FalseClass
62 alias old_to_s to_s
63 def to_s
64 ""
65 end
66 end
67
68 class Integer
69 alias each times
70 include Enumerable
71 end
72
73 class String
74 alias / split
75
76 def to_a
77 split('')
78 end
79
80 (Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth|
81 eval"def #{meth}(*args, &block)
82 a=to_a
83 result = a.#{meth}(*args, &block)
84 replace(a.join)
85 if result.class == Array
86 Integer===result[0] ? result.pack('c*') : result.join
87 elsif result.class == Enumerable::Enumerator
88 result.map(&:join).to_enum
89 else
90 result
91 end
92 end"
93 }
94 end
95
96 class Enumerable::Enumerator
97 alias old_to_s to_s
98 (Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth|
99 eval"def #{meth}(*args, &block)
100 to_a.#{meth}(*args, &block)
101 end"
102 }
103 alias inspect old_to_s
104 end
Something went wrong with that request. Please try again.