Front End Collision

Blog of software engineer Josh Beam

From JavaScript to Ruby: Style Guide

20 April 2015 — Josh Beam — javascript ruby

All Posts

This post is the second in the series of “From JavaScript to Ruby”, which is aimed at helping JavaScript developers transition their thinking from JavaScript to Ruby. Here you’ll find a table of the ways we do things in JavaScript, and the way you’re supposed to do them in Ruby.

Style Guide

This post is a living document. Expect changes as necessary. Suggestions? Leave a comment below or email Josh.

Last updated: Apr 20, 2015

If you do this in JavaScript...
Airbnb
...do it like this in Ruby
bbatsov
String quotes
'Some string' 'Some string'
String concatenation
var world = 'world!',
    helloWorld = 'hello ' + world;
world = 'world'
hello_world = "hello #{world}"
Variable names
camelCaseVariable = true camel_case_variable = false
Tabs
function foo() {
    // 'hard tab', 4 spaces
}
def foo
  # 'soft tab', 2 spaces
end
Callbacks
function someFunction(cb) {
  var hello = 'hello';

  if(typeof cb !== 'undefined') {
    return cb.call(this, hello);
  }
}

someFunction(function (h) {
  return h;
});
def someMethod
  hello = 'hello'
  yield(hello) if block_given?
end

someMethod do |h|
  h
end

# or

someMethod { |h| h }
Talking about methods
someClass.someMethod SomeClass#some_method
Comments
// single-line comment
/*
  multi-line
  comment
*/
# single-line comment
# multi-line
# comment