Documente Academic
Documente Profesional
Documente Cultură
Numbers
Know how to add, subtract, and multiply numbers in Ruby:
3 + 3 == 6 3 * 4 == 12 4 - 3 == 1 8 / 2 == 4
Variables
Know what a variable is and how to store and recall values. A variable stores a value for later use. Know how to create and assign to variables.
# create variable, assign it 3 my_favorite_number = 3
Methods
Know what a method is, how to define one, how to return a value from a method. Know how to call a method.
# block of code that will take in a number, and return its # square def square(num) # squares num; the last expression in a method gets returned # implicitly
square(3) == 9
# search for `target_item` in `items`; return the array index # where it first occurs def find_item(items, target_item) i = 0 while i < items.count current_item = items[i] if current_item == target_item # found item; return its index; stop here and exit the # method return i end i += 1 end
Conditionals
Know how to use if/elsif/else.
def how_big(num) if num < 0 "negative"
elsif num < 10 "pretty small" elsif num < 100 "not too big" elsif num < 1000 "plenty big" else "way too big" end end
Arrays
Know what an array is. Know how to create one. Know how to access and set an element of the array. Know how to add elements to the end of an array. Know how to find the number of elements in an array Arrays are lists of items. You can create an empty array like this
empty_array = []
To fetch an item in the array, use its position. Positions start at zero.
some_primes[0] == 2 # first prime some_primes[1] == 3 # second prime
<< :
count
method:
Loops
Know what a while loop is and how to use one. Know how to use a while loop to loop through the items in an array. A while loop repeatedly executes a block as long as its "condition" is true:
while true # <<< "true" is the condition # loop forever end
To loop through the items in an array, use an "index variable" (often named i ) and go one-by-one through the positions in the array.
i = 0 while i < items.count current_item = items[i]
i += 1 end