All Flashcards
What are variables?
Variables are assigned values that use the = symbol. The value on the left now represents the data on the right. For example, x= “hello” if we type x into the terminal, we will recieve the value “hello”
What does .each do when used in a loop?
.each is a built in iterator. It loops through each item in a list, hash, or other iterable object allowing you to perform operations on that value. The block of an .each statement creates a new scope for your variable so you don’t accidentally modify the original value.
one_to_ten = (1..10).to_a
one_to_ten.each do |num|
print (num**2).to_s + “ “
end
1 4 9 16 25 36 49 64 81 100
What does .times do when used in a loop?
It performs an action a given number of times.
3.times do
puts “I’m in the loop!”
end
puts “I’m out the loop!”
How do we create a ternary statement out of this if/then statement?
grade = 88 if grade >= 70 then puts "pass" else puts "fail"
syntax:
boolean expression ? true expression : false expression.
grade = 88
status = grade >= 70 ? “pass” : “fail”
=> pass
What is a String?
Strings are used for storing and manipulating text. They are written between quotation marks. Both single and double quotes at each end of a string must match.
What’s the difference between puts and prints?
Puts is short for “put string” 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!
What is a method?
A Method is used to create parameterized, reusable code. Ruby methods have to start with a “def” and end with an “end”
def method_name(arguments) # Code to be executed end
def sum(x,y)
x + y
end
What is a while loop?
A while loop will execute a block of code as long as its condition is true. When the condition becomes false, the code after the end of the loop will be executed.
Syntax: while condition_is_true # do something end
Example: i = 1 while i < 5 puts "#{i} is less than 5!" i += 1 end puts "Done!"
1 is less than 5! 2 is less than 5! 3 is less than 5! 4 is less than 5! Done!
What is an Until Loop?
An until loop will execute a block of code as long as its condition is false. When the condition becomes true, the code after the end of the loop will be executed.
Syntax:
until condition_is_false
# do something
end
Example:
counter = 3
until counter
3
2
1
Blast off!
What is a ‘for’ loop?
The for loop is used to iterate an object. The Ruby .each method is preferred over the for loop because the for loop does not create a new scope for the object whereas the .each method does. The for loop is rare in Ruby.
Syntax:
for iterator in iterable_object
# do something
end
Example:
for number in (0..5)
puts number
end
0 1 2 3 4 5
Example: my_array = ["Matz", "chunky", "bacon"] for item in my_array puts item end
Matz
chunky
bacon
How does and if, elsif and else statement work?
If statements are Boolean expressions and are used to in methods and only run if true.
How does an unless statement work?
An unless statement is the opposite of and if statement. It runs if the boolean expression runs false.
True or False: When creating a class, you use CamelCase as the naming convention.
TRUE.
True or False: When you daisy chain methods together, the last one returns your value.
TRUE.
True or False: Classes inherit other classes.
TRUE.
How do you create a new instance of a class?
variable = Class.new
When creating a new instance of a class, and we use .new - what is the naming convention of the data before .new?
The word must be upper case. For example: x = Class.new. That creates a new instance of the class.
What do we use in hashes that represent our data?
keys.
How do we create a new hash?
by assigning a variable to Hash.new. Example: x = Hash.new
How would we assign a father, mother, brother to a hash?
family = Hash.new family = {father: "Mike", mother: "Mary Kay", brother: "John"}
calling a hash can look like this:
family[:husband] = “Christian”
What does array.unshift do?
It adds an item to the beginning of that array.
What does array.pop do?
Pop removes the last element in an array.
What does array.shift do?
It removes the first item in an array.
How do we create a new array?
We use Array.new. for example: x = Array.new