Best Practices

Ruby Style Guide

Comment


# Single line comment 

# Multiple 
# line 
# comment
          

Variable


stuff = "stuff"
          

String


"Single line string"
          

Integer


42
          

Boolean


true
# or 
false
          

Array


["stuff", "things"]
          

Hash


{"stuff_key"=>"stuff_value", "things_key"=>"things_value"}
          

For Loop


# Iterate list
things.each do |thing|
  puts "#{thing}"
end

# alternate syntax
things.each {|thing| puts "#{thing}"}

# Iterate dictionary
stuff.each do |key, value|
  puts "#{key} #{value}"
end
          

While Loop


i = 1
while i < 42 do
  puts(i)
  i += 1
end
          

If, Elsif, Else


if 1 > 42
    puts "One"
elsif 42 < 1
    puts "The answer"
else
    puts "Maths is fun"
end
          

Function


def stuff()
    stuff = ["stuff", "more stuff", "other stuff"]
    return stuff
end
          

Class


class StuffAndThings

    def initialize()
        @stuff = "stuff"
    end

    def things()
        puts "things"
    end
end
          






















Updated: 2017-10-02