Code golf tips of ruby.
There isn't need whitespace in CodeGolf.
# bad
puts "hello world"
# good
puts"hello world"
There isn't need parentheses in CodeGolf.
# bad
puts("hello")
[1,2,3].map(&:to_s)
# good
puts"hello"
[1,2,3].map &:to_s
Variables should be defined with one character.
# bad
word='ruby'
# good
w='ruby'
if same value, variables can be declared together.
# bad
a=3
b=3
# good
a=b=3
a,b,c=[1,2,3] #=> a=1, b=2, c=3
a,b,*c=[1,2,3,4,5] #=> a=1, b=2, c=[3,4,5]
a,*b,c=[1,2,3,4,5] #=> a=1, b=[2,3,4], c=5
a,*b=1 #=> a=1, b=[]
if $
is placed at the beginning of a variable name, it becomes a global variable.
Calling undefined global variables will only return nil
and no error will occur.
$a #=> nil
p
method return nil
if doesn't output anything.
p p #=> nil
use methods which name shorter in code golf.
# bad
[1,2,3].find_index(2) #=> 1
# good
[1,2,3].index(2) #=> 1
# bad
10.times do|n|puts"#{n} hello"end
# good
10.times{|n|puts"#{n} hello"}
In the Windows environment, because the newline code is defined with \r\n
, reduce the number of lines.
# bad
puts'hello'
puts'world'
# good
puts'hello';puts'world'
TBD
replace comparison operators ==
, <=
and >=
with >
or <
if possible.
# bad
n<=3
# good
n<4
There are patterns that are shortened by using ...
# bad
(0..n-1).each{|n|p n}
# good
(0...n).each{|n|p n}
Use short-circuit evaluation and treat logical operators &&
and ||
as conditional expressions.
# bad
if true
n=3
end
# good
true&&n=3
# bad
unless false
n=3
end
# good
false||n=3
# bad
if 3>2
333
else
777
end
# good
3>2?333:777
# bad
a=[] #=> []
# good
$* #=> []
# bad
ARGV
# good
$*
# bad
"\n"
# good
$/
It is important to shorten a processing of input.
bad
while gets
puts $_
end
Add option 「-n」, each input line is inserted into variable $_.
good
#!ruby -n
puts $_
Add option 「-p」, At the end of program, execute 「puts $_」
very good
#!ruby -np
$a=3
# bad
puts "$a is #{$a}"
# good
puts "$a is #$a"
# bad
w="Hello Ruby!"
puts w
# good
w="Hello Ruby!"
$><<w
# bad
10.times{puts:hello}
# good
puts [:hello]*10
$s="Ruby"
# bad
puts $s
# good
puts$s
# bad
puts 100
# good
p 100
# bad
'hello world ruby'.split(' ') #=> ['hello', 'world', 'ruby']
# good
'hello world ruby'.split #=> ['hello', 'world', 'ruby']
# bad
"ruby".split('') #=> ["r", "u", "b", "y"]
# good
"ruby".chars #=> ["r", "u", "b", "y"]
# bad
c='a'
# good
c=?a
# bad
"hello\n".size-1 #=> 5
# good
"hello\n"=~/$/ #=> 5
# bad
puts"ruby"
# good
puts:ruby
# bad
if 'ruby'=~/r/
puts 'hello!'
end
# good
if 'ruby'[?r]
puts 'Hello!'
end
s="Hello Ruby"
# bad
s.gsub(/Ruby/){|m|puts m} #=> Ruby
# good
s.gsub(/Ruby/){puts $&} #=> Ruby
'hello'*0 #=> ''
'hello'*1 #=> 'hello'
'hello'*2 #=> 'hellohello'
# bad
n=10000
# good
n=1e4 #=> 10000.0
# bad
n=0.0001
# good
n=1e-4 #=> 0.0001
# bad
a=-10
# good
a=~9
# bad
a=n==0?1:0
# good
a=1[n]
to_s
can specify radix when convert integer to string.
16.to_s #=> "16"
16.to_s(2) #=> "10000"
16.to_s(8) #=> "20"
16.to_s(16) #=> "10"
[]
method can get bit value.
p 1[0] #=> 1
p 2[0] #=> 0
p 3[0] #=> 1
n=3
# bad
(n+1)*3 #=> 12
# good
-~n*3 #=> 12
n=3
# bad
(n-1)*3 #=> 6
# good
~-n*3 #=> 6
# bad
a=Array.new
# good
a=[]
# bad
a=['hoge','piyo','fuga']
# good
a=%w(hoge piyo fuga)
# bad
a=[1,2,3,4,5]
# good
a=*1..5
# bad
[1,2,3].join('+') #=> 1+2+3
# good
[1,2,3]*?+ #=> 1+2+3
# bad
[1,1,2,2,3,3].uniq #=> [1,2,3]
# good
[1,1,2,2,3,3]|[] #=> [1,2,3]
# bad
[1,2,3].push(4) #=> [1, 2, 3, 4]
# good
[1,2,3]<<4 #=> [1, 2, 3, 4]
# bad
[1,2,3].unshift(4) #=> [4, 1, 2, 3]
# good
[1,2,3][0,0]=4 #=> [4, 1, 2, 3]
p [nil,1,2,3,nil].compact #=> [1, 2, 3]
p [nil,1,2,3,nil]-[p] #=> [1, 2, 3]
a=[1,2]
# bad
a.reverse #=> [2, 1]
# good
a.rotate #=> [2, 1]
# bad
[1,2,3].inject(:+) #=> 6
# good
eval"[1,2,3]*?+" #=> 6
# very good (RUBY_VERSION >= 2.4)
[1,2,3].sum
update max value
# bad
a=[a,b+1].max
# good
a>b||a=b+1
n="10"
t="1"
i=0
# bad
n.to_i.times{i+=t.to_i}
# good
eval"i+=#{t};"*n.to_i
check odd? or even?
h={}
h[1]^=1 #=> nil^1 -> true
h[1]^=1 #=> true^1 -> false