Skip to content

siman-man/ruby-golf-style-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 

Repository files navigation

Code Golf tips of ruby

Code golf tips of ruby.

Remove whitespace.

There isn't need whitespace in CodeGolf.

# bad
puts "hello world"

# good
puts"hello world"

Remove parentheses

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

Variable

Variables should be defined with one character.

define

# bad
word='ruby'

# good
w='ruby'

if same value, variables can be declared together.

# bad
a=3
b=3

# good
a=b=3

Multiple assignment

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=[]

Global variable

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

Get nil with one character

p method return nil if doesn't output anything.

p p #=> nil

Use shorter method name

use methods which name shorter in code golf.

# bad
[1,2,3].find_index(2) #=> 1

# good
[1,2,3].index(2) #=> 1

Block use {...} instead of do end

# bad
10.times do|n|puts"#{n} hello"end

# good
10.times{|n|puts"#{n} hello"}

Reduce newline codes

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'

Read the document

TBD

Comparison operator is one character

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}

Treat logical operators as conditional expressions

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

Ternary operator

# bad
if 3>2
  333
else
  777
end

# good
3>2?333:777

Special

# bad
a=[] #=> []

# good
$* #=> []

# bad
ARGV

# good
$*

# bad
"\n"

# good
$/

Command Line Option

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

Global

$a=3

# bad
puts "$a is #{$a}"

# good
puts "$a is #$a"

STDIO

Output

# 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

Output(Numeric)

# bad
puts 100

# good
p 100

String

split

# 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"]

char

# bad
c='a'

# good
c=?a

size

# bad
"hello\n".size-1 #=> 5

# good
"hello\n"=~/$/ #=> 5

symbol

# bad
puts"ruby"

# good
puts:ruby

match

# 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

other tips

'hello'*0 #=> ''
'hello'*1 #=> 'hello'
'hello'*2 #=> 'hellohello'

Numeric

define

# 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]

Radix conversion

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"

get bit value

[] method can get bit value.

p 1[0] #=> 1
p 2[0] #=> 0
p 3[0] #=> 1

Bit operation

n=3

# bad
(n+1)*3 #=> 12

# good
-~n*3 #=> 12

n=3

# bad
(n-1)*3 #=> 6

# good
~-n*3 #=> 6

Array

Literal

# 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

join

# bad
[1,2,3].join('+') #=> 1+2+3

# good
[1,2,3]*?+ #=> 1+2+3

uniq

# bad
[1,1,2,2,3,3].uniq #=> [1,2,3]

# good
[1,1,2,2,3,3]|[] #=> [1,2,3]

push

# bad
[1,2,3].push(4) #=> [1, 2, 3, 4]

# good
[1,2,3]<<4 #=> [1, 2, 3, 4]

unshift

# bad
[1,2,3].unshift(4) #=> [4, 1, 2, 3]

# good
[1,2,3][0,0]=4 #=> [4, 1, 2, 3]

compact

p [nil,1,2,3,nil].compact #=> [1, 2, 3]
p [nil,1,2,3,nil]-[p]     #=> [1, 2, 3]

reverse

a=[1,2]

# bad
a.reverse #=> [2, 1]

# good
a.rotate #=> [2, 1]

sum

# bad
[1,2,3].inject(:+) #=> 6

# good
eval"[1,2,3]*?+" #=> 6

# very good (RUBY_VERSION >= 2.4)
[1,2,3].sum

other

update max value

# bad
a=[a,b+1].max

# good
a>b||a=b+1

Enumerator

n="10"
t="1"
i=0

# bad
n.to_i.times{i+=t.to_i}

# good
eval"i+=#{t};"*n.to_i

Hash

other

check odd? or even?

h={}
h[1]^=1 #=> nil^1  -> true
h[1]^=1 #=> true^1 -> false

About

Code golf tips of ruby.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published