Mark's Ruby on Rails Cards Flashcards
string
Strings are used for storing and manipulating text in Ruby. Strings are written between quotation marks.
my_name = "Eric" that_computer = "Eric's Computer" #this is legal
booleans
In Ruby, there are two boolean values: true and false.
true
=> true
false
=> false
variables
Variables are assigned values using the = operator.
Syntax
variable_name = value
Example
name = “Artur”
=> “Artur”
name_copy = name
=> “Artur”
age = 21
=> 21
puts vs. print
The puts (short for “put string”) and print commands are both used to display the results of evaluating Ruby code. The primary difference between them is that puts adds a newline after executing, and print does not.
3.times { print “Hello!” }
Hello!Hello!Hello!
3.times { puts “Hello!” }
Hello!
Hello!
Hello!
Methods
A Ruby method is used to create parameterized, reusable code. Ruby methods can be created using the syntax:
Syntax
def method_name(arguments)
# Code to be executed
end
Example
def sum(x,y)
x + y
end
sum(13, 379)
=> 392
A method, then, is simply programming jargon for something one object can do for another.
Multi Line Command
You can write multi-line comments by starting the comment with =begin and finishing it with =end
Alternatively, each line can start with a #
gets
gets is the Ruby method that gets input from the user. When getting input, Ruby automatically adds adds a blank line (or newline) after each bit of input; chomp removes that extra line (Your program will work fine without chomp, but you’ll get extra blank lines everywhere
array
In Ruby, we can pack multiple values into a single variable using an array. An array is just a list of items between square brackets, like so: [1, 2, 3, 4]. The items don’t have to be in order—you can just as easily have [10, 31, 19, 400].
if
Ruby’s if statement takes an expression, which is just a fancy word for something that has a value (like 4, true, or pants). If that expression is true, Ruby executes the block of code that follows the if. If it’s not true (that is, false), Ruby doesn’t execute that block of code: it skips it and goes on to the next thing.
Here’s an example of an if statement in action:
if 1 < 2
print “I’m getting printed because one is less than two!”
end
unless
This is the opposite of an if statement. The statement takes a boolean expression and executes certain code only if the boolean expression evaluates to false.
Syntax
unless boolean_expression
#do something here
end
Example
hungry = true
unless hungry puts "I'm writing Ruby programs!" else puts "Time to eat!" end
elsif
A conditional statement used to manage a program’s control flow. The statement must be paired with an if or unless block and takes a boolean expression. It runs certain code only if the previous conditional statements do not run and its boolean expression evaluates to true. it is equivalent to writing an else statement that has an if statement in its block.
Syntax
if boolean_expression #do something elsif boolean_expression_2 #do something different else #do something else
Example
x = 5 if x > 5 print "I am big!" elsif x == 5 print "I am medium!" else print "I am small!" end
I am medium!
else
The partner to the if statement is the else statement. An if/else statement says to Ruby: “If this expression is true, run this code block; otherwise, run the code after the else statement.” Here’s an example:
if 1 > 2 print "I won't get printed because one is less than two." else print "That means I'll get printed!" end
environment
The collection of all variables and their values that exist in the program at a given time.
Control flow
Control flow gives us the flexibility we’re looking for. We can select different outcomes depending on information the user types, the result of a computation, or the value returned by another part of the program.
expression
Ruby’s if statement takes an expression, which is just a fancy word for something that has a value (like 4, true, or pants). If that expression is true, Ruby executes the block of code that follows the if. If it’s not true (that is, false), Ruby doesn’t execute that block of code: it skips it and goes on to the next thing.
Here’s an example of an if statement in action:
if 1 < 2
print “I’m getting printed because one is less than two!”
end
Ruby doesn’t care about whitespace (spaces and blank lines), so the indentation of the print statement isn’t necessary. However, it’s a convention that Rubyists (Ruby enthusiasts) follow, so it’s good to get in the habit now. The block of code following an if should be indented two spaces.
When you’re done with your if, you have to tell Ruby by typing end.
assignment operator
In Ruby, we assign values to variables using =, the assignment operator.But if we’ve already used = for assignment, how do we check to see if two things are equal? Well, we use ==, which is a comparator (also called a relational operator). == means “is equal to.” When you type
x = 2 y = 2 if x == y print "x and y are equal!" end
you’re saying: “if x equals y, print ‘x and y are equal!’” You can also check to see if two values are not equal using the == comparator.