Work In Progress

Work on this post is underway... It will be done "soon..."


The basics

Variables

Variables are named in snake_case. Variables are not typed.

puts is your goto method to print to the console.

my_first_variable = "Hello world!"
puts my_first_variable

Note that there is a print as well, it is similar to puts except that it doesn’t add a new line to the output.

Comments

Single line comments starts with #. Multi-line comments are surrounded by =begin ... =end

# single line comment

=begin
Line 1
Line 2
=end

Naming Convention

  • variable names snake_case
  • class names CamelCase
  • Constants UPPER_SNAKE_CASE or CamelCase (be consistent and check your team conventions)

Parentheses

Optional, generally if you have a method with multiple arguments it is a good idea to keep them for readability purposes. Omit them for Control statements conditions. For commonly used methods are left by convention. puts is a good example, but you’ll need to pick that up from the community. In general, leave them out except for method arguments.

Multi-Statement Lines

You can have multiple statements in one line separated by ;. Use that only in simple cases where a class definition is three lines or for an abstract method.

Code Blocks

You can use either {} or do ... end to surround code blocks. The convention is to use {} if the block consists of a single statement (one liner), and do ... end otherwise.

Readability… Use Your Judgement

The purpose of the coding convention is readability, so if following one of the above breaks readability, use your judgement… sparingly! Remember consistency is important.

Expressions not statements

Almost everything in Ruby is an expression. For example an if statement is an expression that has the value returned by the last expression in that if

some_value = if true then 4 else 6 end
puts some_value # This puts `4`

Conditionals

  • Only false and nil are treated as false

if, else, elseif and unless

There is the if statement similar to any other language you came from

something_evil_lurking_in_the_dark = false

if something_evil_lurking_in_the_dark
  puts "Run away!"
end

Now, let’s negate that

if not something_evil_lurking_in_the_dark
  puts "We are safe!"
end

Ruby has an alternative to if not which is unless

unless something_evil_lurking_in_the_dark
  puts "We are safe!"
end

We can also use the good old else and elseif. While we can use those with unless, it doesn’t make much sense because you can simply reverse your if statement

if something_evil_lurking_in_the_dark
  puts "Run away..."
else
  puts  "We are safe!"
end

This last example can be written as a one liner, but we will need a new keyword then

if something_evil_lurking_in_the_dark then puts "Run away..." else  puts "We are safe!" end

Ternary operator

THe last example above can be turned to one liner

something_evil_lurking_in_the_dark ? (puts "Run away..." ): (puts "We are safe!")

Modifier form

Ruby has a modifier form of its conditionals. What that does is reversing the order of the statement to make it more “readable” or rather look like the common English language

puts "We are safe!" unless something_evil_lurking_in_the_dark

Even further, we can use the same form with assignment

message = "We are safe"
message = "Run away!!" if something_evil_lurking_in_the_dark

The value of message will change only if the value of something_evil_lurking_in_the_dark is true.

Case

case the_thing_lurking_in_the_dark
when 'monster'
    puts 'Run away'
when 'cat'
    puts 'sigh of relief'
else # default case
    puts 'Don't know what that is'

this is the same as

response = case the_thing_lurking_in_the_dark
    when 'monster'
        'Run away'
    when 'cat'
        'sigh of relief'
    else
        'Don't know what that is?'
puts        

Which is also the same as

response = case title
    when 'monster' then 'Run away'
    when 'cat' then 'sigh of relief'
    else 'Don't know what that is?'
puts response        

We can use case to match on types

case some_data
when Monster
    puts "this is a string"
when Cat
    puts "this is a cat"
else
    puts "I don't know what is this!"

This will result in a nil if no when clause is matched and there is no else

Collections

while and until

  • We should iterate with each instead of while and until.
  • until acts like while not. Both, similarly, can be used in the modifier form.

Iterating with each

When iterating over loops we can use each instead of for loop to go through the elements of a collection. Actually, Ruby under the hood would turn for statement into an each statement.

array.each do |element|
  puts element
end

If we want to look at the index as well, we can use each_with_index. each_index is another method that would let us iterate over indices.

array.each_with_index do |element, i|
  puts "#{i}: #{element}"
end
array.each_index do |i|
  puts i
end

Caveats

  • defined? never returns true it either a string that describe the thing passed in or nil

Clever idioms

  • x ||= y will only set x to y if x is not defined. This is equivalent to x = x || y

Resources