- 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.
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.
# by using the ‘#’ sign on every
# single line=begin
by enclosing my comments in a set of
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.
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.
a = “letter”
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.
x = true
y = false
on = true
off = false
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.
dog = ‘clean’
dog = ‘dirty’
Let’s do some arithmetic. In Ruby, you can add, subtract, multiply, divide numbers easily.
a = 1 + 2
b = 10 – 8
c = 7 * 3
d = 8 / 2
You can also use exponents!
eight = 2 ** 3
hundred = 10 ** 2
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.
even_or_odd = 3 % 2
even_or_odd = 8 % 2
even_or_odd = 124545234 % 2
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