mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* golf_prelude.rb: syntax formatting for whitespace [Fixes GH-425]
Patch by @edward https://github.com/ruby/ruby/pull/425 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a6698bc676
commit
b393aeb055
2 changed files with 24 additions and 18 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Fri Nov 8 01:01:54 2013 Zachary Scott <e@zzak.io>
|
||||||
|
|
||||||
|
* golf_prelude.rb: syntax formatting for whitespace [Fixes GH-425]
|
||||||
|
Patch by @edward https://github.com/ruby/ruby/pull/425
|
||||||
|
|
||||||
Thu Nov 7 19:36:09 2013 Koichi Sasada <ko1@atdot.net>
|
Thu Nov 7 19:36:09 2013 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* gc.c: modify malloc_limit strategy.
|
* gc.c: modify malloc_limit strategy.
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
class Object
|
class Object
|
||||||
@@golf_hash = {}
|
@@golf_hash = {}
|
||||||
|
|
||||||
def method_missing m, *a, &b
|
def method_missing m, *a, &b
|
||||||
t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]
|
t = @@golf_hash[ [m, self.class] ] ||= matching_methods(m)[0]
|
||||||
if t && b
|
if t && b
|
||||||
__send__(t, *a) {|*args|
|
__send__(t, *a) {|*args|
|
||||||
b.binding.eval("proc{|golf_matchdata| $~ = golf_matchdata }").call($~) if $~
|
b.binding.eval("proc{|golf_matchdata| $~ = golf_matchdata }").call($~) if $~
|
||||||
|
@ -12,27 +13,27 @@ class Object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def matching_methods(s='', m=callable_methods)
|
def matching_methods(s = '', m = callable_methods)
|
||||||
r=/^#{s.to_s.gsub(/./){"(.*?)"+Regexp.escape($&)}}/
|
r = /^#{s.to_s.gsub(/./){"(.*?)" + Regexp.escape($&)}}/
|
||||||
m.grep(r).sort_by do |i|
|
m.grep(r).sort_by do |i|
|
||||||
i.to_s.match(r).captures.map(&:size) << i
|
i.to_s.match(r).captures.map(&:size) << i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.const_missing c
|
def self.const_missing c
|
||||||
t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c,constants)[0]
|
t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c, constants)[0]
|
||||||
t and return const_get(t)
|
t and return const_get(t)
|
||||||
raise NameError, "uninitialized constant #{c}", caller(1)
|
raise NameError, "uninitialized constant #{c}", caller(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def shortest_abbreviation(s='', m=callable_methods)
|
def shortest_abbreviation(s = '', m = callable_methods)
|
||||||
s=s.to_s
|
s = s.to_s
|
||||||
our_case = (?A..?Z)===s[0]
|
our_case = (?A..?Z) === s[0]
|
||||||
if m.index(s.to_sym)
|
if m.index(s.to_sym)
|
||||||
1.upto(s.size){|z|s.scan(/./).combination(z).map{|trial|
|
1.upto(s.size){|z| s.scan(/./).combination(z).map{|trial|
|
||||||
next unless ((?A..?Z)===trial[0]) == our_case
|
next unless ((?A..?Z) === trial[0]) == our_case
|
||||||
trial*=''
|
trial *= ''
|
||||||
return trial if matching_methods(trial,m)[0].to_s==s
|
return trial if matching_methods(trial, m)[0].to_s == s
|
||||||
}}
|
}}
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
|
@ -45,7 +46,7 @@ class Object
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def h(a='H', b='w', c='!')
|
def h(a = 'H', b = 'w', c = '!')
|
||||||
puts "#{a}ello, #{b}orld#{c}"
|
puts "#{a}ello, #{b}orld#{c}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -84,14 +85,14 @@ class String
|
||||||
split('')
|
split('')
|
||||||
end
|
end
|
||||||
|
|
||||||
(Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth|
|
(Array.instance_methods - instance_methods - [:to_ary, :transpose, :flatten, :flatten!, :compact, :compact!, :assoc, :rassoc]).each{|meth|
|
||||||
eval"
|
eval "
|
||||||
def #{meth}(*args, &block)
|
def #{meth}(*args, &block)
|
||||||
a=to_a
|
a = to_a
|
||||||
result = a.#{meth}(*args, &block)
|
result = a.#{meth}(*args, &block)
|
||||||
replace(a.join)
|
replace(a.join)
|
||||||
if result.class == Array
|
if result.class == Array
|
||||||
Integer===result[0] ? result.pack('c*') : result.join
|
Integer === result[0] ? result.pack('c*') : result.join
|
||||||
elsif result.class == Enumerator
|
elsif result.class == Enumerator
|
||||||
result.map(&:join).to_enum
|
result.map(&:join).to_enum
|
||||||
else
|
else
|
||||||
|
@ -103,8 +104,8 @@ end
|
||||||
|
|
||||||
class Enumerator
|
class Enumerator
|
||||||
alias old_to_s to_s
|
alias old_to_s to_s
|
||||||
(Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth|
|
(Array.instance_methods - instance_methods - [:replace] + [:to_s]).each{|meth|
|
||||||
eval"
|
eval "
|
||||||
def #{meth}(*args, &block)
|
def #{meth}(*args, &block)
|
||||||
to_a.#{meth}(*args, &block)
|
to_a.#{meth}(*args, &block)
|
||||||
end"
|
end"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue