Ruby Projects: Basics

The Basics

Program Structure

  • Program Description – It’s helpful to know what your program will be doing!
  • Declarations/Initialization – This is where you will create and set up everything you will need to make your program run.
  • Work – Do something!
  • Results/Output – Show the user the results of the work.
Whitespace and Comments
In Ruby, whitespace is used to structure code. Whitespace is important, so you have to be careful with how you use it. For example, whitespace matters for different data structures, like ‘for’ and ‘while’ loops. We will learn more about these later. Whitespace is also useful for breaking up code into more readable parts.A comment is a line of text that Ruby won’t try to run as code. It’s just for humans to read. Comments, like whitespace, can make your program easier to understand. When you look back at your code or others want to collaborate with you, they can read your comments and easily figure out what your code does.The ‘#’ sign will only comment out a single line. You can write multi-line comments using ‘#’ symbol, but that can be tedious. Instead, for multi-line comments, you can include the whole block in a set of =begin and =end keywords.
Examples:# This is a single line comment.# I can make a multi-line comment
# by using the ‘#’ sign on every
# single line=begin

Or I can make a multi-line comment
by enclosing my comments in a set of
specific keywords.
=end
Displaying information
In Ruby, there are two different methods for printing information to the screen: print and puts. The primary difference between them is that puts adds a newline after executing, and print does not.
Examples:
print “This is a test!”
print (“This is a second test!”)
puts “This is also a test!”
puts (“Guess what! This is another test!”)Variables
Creating webpages, apps, search engines, and security protocols all involve storing and working with different types of data. They do so using variables. A variable stores a piece of data, and gives it a specific name.Variable names:

  • Must begin with a letter (a – z, A – B) or underscore (_)
  • Other characters can be letters, numbers or _
  • Case Sensitive
  • Can be any length
  • There are some reserved words which you cannot use as a variable name because Ruby uses them for other things. Reserved words will usually be colored in your Ruby program.
Examples:green_chile = “good”red_chile = 5burrito = 30

a = “letter”

Sometimes you will want to print variables to the screen in the middle of a sentence. You will need to use the following format:
name = “Jen”
puts “Hello! My name is #{name}.”

 

 

Booleans
A boolean is like a light switch. It can only have two values. Just like a light switch can only be on or off, a boolean can only be true or false.

Examples:

x = true

y = false

on = true

off = false

 

Variable reassignment
Just because you initialize a variable to a certain value, doesn’t mean that it has to hold that value forever. You can change the value of a variable by “reassigning” it.

Examples:

dog = ‘clean’

puts dog

dog = ‘dirty’

puts dog

 

Math
Let’s do some arithmetic. In Ruby, you can add, subtract, multiply, divide numbers easily.

Examples:

a = 1 + 2

b = 10 – 8

c = 7 * 3

d = 8 / 2

 

Exponents
You can also use exponents!

Examples:

eight = 2 ** 3

hundred = 10 ** 2

 

Modulus
Another important operator is the modulus symbol. It returns the remainder from a division operation. Modulus is most useful for determining when a number is even or odd. If a modulus operation returns a 0, a number is even. If a modulus operation returns a 1, a number is odd.

Examples:

even_or_odd = 3 % 2

puts (even_or_odd)

even_or_odd = 8 % 2

puts (even_or_odd)

even_or_odd = 124545234 % 2

puts (even_or_odd)

 

Practice what you’ve just learned!
Download the file “RubyBasics.rb” below. Open the file with Notepad++ and follow the instructions to complete the program