Ruby Coding Convention
Ruby Coding Convention
File Names
ex.
Foo class => foo.rb
Bar module => bar.rb
FooBar class => foobar.rb (Normal Rule)
foo_bar.rb (Rails Rule)
Foo::Bar class => foo/bar.rb
*1
Libraries
non-standard multiple files required by a script should be in one directory, and they should
be installed to the standard site-ruby directory or a script's library directory.
don't use a current directory for libraries --- it is dangerous. since the directory in which
script exists can not be specified portably, it is also not suited for libraries.
File Organization
• require (if necessary)
• include (if necessary)
• classes and modules definition
• main
• testing code(?) using __FILE__ idiom
ex:
if __FILE__ == $0
<testcode>
end
Beginning Comment
=begin
* Name:
* Description
* Author:
* Date:
* License:
=end
# bar.bar.bar.
#
class Bar
# constructor of Bar.
#
def initialize(bar = nil)
super(bar)
end
def bar
self
end
end
def initialize()
end
end
Indentation
Line length
Wrapping Line
Comments
Ruby has two comments style: =begin...=end and '#'. You should use =begin...=end for
Documentation Comments, and '#' for both Documentation Comments and Implementation
Comments.
Implementation Comments
Block Comments
Single-Line Comments
Tailing Comments
if a == 2
true # special case
else
prime?(a) # work only for odd a
end
Documentation Comments
Header/Footer
=begin
= FooBar library
== What's New?
.....
....
== Installation
.....
....
....
=end
In Class/Module
# .....
# ....
#
def foo()
..
or
##
# .....
# ....
#
def foo()
..
Way of no commenting
If you can write simple, short and light scripts, comments may not be necessary.
You can let the script itself tell everything, instead of embedding documentation that may
confuse readers of your script.
Definitions
Initialization
level = 0
size = 0
is preferred over
level = size = 0
Placement
Statements
Simple Statements
foo = 1 ## Correct
bar = 2 ## Correct
Compound Statements
simple example:
if <condition>
<statements>
end
if <condition>
<statements>
elsif <condition>
<statements>
else
<statements>
end
<statements> if <condition>
block methods
`{...}' style:
bar.foo(vars){|vars2|
<statements>
}
`do...end' style:
bar.foo(vars) do |vars2|
<statements>
end
one-line block:
bar.foo(){|var| <statements> }
case-when Statements
case foo
when condition1
<statements>
when condition2
<statements>
else
<statements>
end
begin-rescue-end Statements
White Space
Blank Lines
Blank spaces
ex:
while (foo.end?) {
<statements>
}
a+b ## Correct
a + b ## Correct
a+ b ## AVOID!
a +b ## AVOID! (Erroneous: interpreted as a(+b))
a += b + c
a = (a + b) / (c * d)
a = b
foo("foo" + buz + bar)
Naming Conventions
Classes/Modules
class and module names should be nouns; in mixed case with the first letter of each internal
word capitalized.
Methods should be verbs. All lower case ASCII letters with words separated by underscores
('_')
Variables
variable names should be all lower case ASCII letters with words separated by underscore
('_')
ex:
i = 1
some_char = SomeChar.new()
table_width = 0.0
Constants
*2
constants should be all upper case with words separated by underscores ('_').
ex:
MIN_LENGTH = 1
DEFAULT_HOST = "foo.example.com"
Omission
Speaking of 'Connection Pool' as a variable, you should decide to prefer name by scope such
as the following...
• 'conpool' for local scope (such as local variable)
• '@connection_pool' for class scope (such as instance variable)
def foo()
@foo
end
attr_reader :foo
Without Parenthesis
• include
ex. include FooModule
• p
ex. p foo
• attr_*
Reduce repetition
x = ModuleA::ClassB::method_c( a )
y = ModuleA::ClassB::method_d( b )
cb = ModuleA::ClassB
x = cb::method_c( a )
y = cb::method_d( b )
include ModuleA
x = ClassB::method_c(a)
y = ClassB::method_d(b)
Code Example
=begin
blahdy/blah.rb
$Id:$
This is free software; you can copy and distribute and modify
this program under the term of Ruby's License
(http://www.ruby-lang.org/LINCENSE.txt)
=end
#
# module description goes here.
#
# @version: 1.82
# @author: TAKAHASHI Masayoshi
#
module Blahdy
end
end
*1
any arguments?
*2
Huh, is there a reasonable background to distinguish constants from a class name which is a constant at the
same time?