Basic Syntax

Declaring Variable Names

In ruby, when declaring variable names always use snake_case, not camelCase. Each time you need to name something that requires multiple words, separate those words using underscores _, and make the whole title lowercase. Names should never start or contain uppercase letters.

Comments

In ruby a single line comment looks like this:

# this is a single line comment

A multiple line comment looks like this:

=begin
This is a multiple line comment
This is line number two
This is line number three
=end

Symbols

Symbols are primarily used as hash keys, or to reference method names. Once a symbol is created, it can not be changed, and only one copy of a symbol can exist at any given time. There can not be multiple symbols with the same value. Symbols look like this: :symbol. They have a colon, and then are followed by a name.

Prints, Puts, Return, & Yield

Print:

The print command takes whatever instructions you give it, and puts it on the screen, on the same line.

print "Hello world!"
print "Hello world!"

Output:

"Hello world!Hello world!"

Puts:

The puts command creates a new line for each thing that you have it print.

puts "Hello world!"
puts "Hello world!"

Output:

"Hello world!"
"Hello world!"

Return:

The return command just returns the value of something. If you don’t tell ruby what to return, it will always return the last expression in the method code block.

def double(n)
  return n * 2
end

output = double(6)
output += 2
puts output
# puts ==> 14

In the example above, we defined a method called double. Inside the method, we return n * 2. We then set a variable output which is equal to double(6). with the argument of 6. We then add two to output, and we puts the variable output. If you don’t tell ruby what to return, it will always return the result of the last expression in the method code block.

Yield:

The yield command allows for methods (that don’t have the capability already) to accept a block of code. Methods that accept blocks have a way of transferring control from the method to the block and back to the method again. You can build this into methods by using the yield command.

def block_test
    puts "We're in the method!"
    puts "Yielding to the block…"
    yield
    puts "We're back in the method!"
end

block_test { puts "--- We're in the block!" }

Output:

"We’re in the method!"
"Yielding to the block…"
"--- We’re in the block!"
"We’re back in the method!"

You can also pass parameters to the yield command.

def yield_name(name)
    puts "In the method! Let's yield."
    yield("Kim")
    puts "In between the yields!"
    yield(name)
    puts "Block complete! Back in the method."
end

yield_name("Eric") { |n| puts "My name is #{n}." }

Output:

"In the method! Let’s yield!"
"My name is Kim."
"In between the yields!"
"My name is Eric."
"Block complete! Back in the method!"

The yield_name method is defined with one parameter, name. On line 8, we call the yield_name method and supply the argument "Eric" for the name parameter. Since yield_name has a yield statement, we will also need to supply a block. Inside the method, on line 2, we first puts an introductory statement. Then we yield to the block and pass in "Kim". In the block, n is now equal to "Kim" and we puts out "My name is Kim." Back in the method, we puts out that we are in between the yields. Then we yield to the block again. This time, we pass in "Eric" which we stored in the name parameter. In the block, n is now equal to "Eric" and we puts out "My name is Eric." Finally, we puts out a closing statement.

Last updated