evaluates to new reversed version of string or array
Back
while condition
Front
loop that stops when condition evaluates to false, must be ended with end
Back
arr.join("")
Front
will make a string of an array by adding the string in split between elements of the array
Back
<<
Front
shovel in new element to end of array
Back
create a new method
Front
def method (param1, param2, ...)
body of method
return # desired value
end
Back
conditionals
Front
if
#elsifs if needed
elsif
elsif
elsif
#else after if and all elsifs if needed
else (encompasses all non if/elsif conditions)
Back
>=
Front
check if one number is greater than/equal to another
Back
create empty array
Front
arr = []
Back
/
Front
divide two numbers
Back
str/arr[startIdx..endIdx]
Front
will return a new string/array with elements from startIdx to endIdx (inclusive)
Back
<
Front
check if one number is less than another
Back
str/arr.reverse!
Front
changes str/arr to its reversed version
Back
<=
Front
check if one number is less than/equal to another
Back
str.index(" ")
Front
returns starting index of quoted string
Back
next
Front
skips to the next iteration of a loop
Back
%
Front
get remainder of two numbers
Back
create empty string
Front
str = ()
Back
str.split("")
Front
will split up the string into an array of multiple strings by separating at the string in split
Back
!=
Front
check if two elements do not equal one another
Back
str/arr[startIdx...endIdx]
Front
will return a new string/array with elements from startIdx to endIdx (not including the char/ele at endIdx)
Back
break
Front
immediately exits loop
Back
create empty hash
Front
hsh = {}
Back
x += n
Front
replaces the value of variable x with the value x+n
Back
.pop
Front
takes in no parameters, removes last element of an array. evaluates to that element.
Back
iteration
Front
what we call each cycle of a loop
Back
.shift
Front
takes in no parameters, removes first element in an array. evaluates to that element.
Back
calling a method with no parameters
Front
just type method with no parentheses
Back
negative indices
Front
will evaluate to ele/char starting at end of array/string, where arr[-1]/str[-1] evaluate to the final element/character respectively
Back
.length
Front
returns length of string
returns size of array
returns number of key/value pairs in hash
Back
str.include?(" ")
Front
returns true if quoted string is substring of string
Back
.length-1
Front
index of last char in string
index of last element of an array
Back
arr.index(" ")
Front
where string is in quotes, returns index of string
Back
calling a method that takes in parameters
Front
type method(arg1, arg2, ...)
Back
arguments
Front
particular data values that we pass into a method when called
Back
-
Front
subtract two numbers
Back
iterate over each element of array using enumerable
Front
arr.each {|ele| <what to do>}
arr.each_with_index{|ele,idx| <what to do>}
arr.each do |ele|
<what to do>
end
arr.each_with_index do |ele,idx|
<what to do>
end
Back
puts
Front
show formatted output
Back
parameters
Front
concrete names that hold data in a method definition
Back
+
Front
add two numbers, concatenate two strings
Back
!
Front
not
Back
[0]
Front
index of the first char in a string
index of the first element of an array
Back
arr.include?(" ")
Front
returns true if array include the quoted string
Back
&&
Front
and
Back
.unshift
Front
used as arr.unshift(ele), adds ele to front of an array. evaluates to the array.
Back
*
Front
multiply two numbers, repeat string contents
Back
==
Front
check if two elements equal one another
Back
||
Front
or
Back
.push
Front
used as arr.push(ele1,ele2,...), adds elements to end of an array. evaluates to the array.
Back
#
Front
comment out a line of code
Back
>
Front
check if one number is greater than another
Back
Section 2
(15 cards)
2d array
Front
an array that holds a set of arrays
Back
iterate over a range of numbers
Front
(start..end).each{|num| <what to do>} iterates inclusive
(start...end).each{|num| <what to do>} iterates excluding end
Back
basic counter hash
Front
to count how many times each char occurs in a string str:
count = Hash.new(0)
str.each_char{|char| count["char"] += 1}
Back
=>
Front
a rocket, assigns a key to a value in a hash
Back
iterate over each element of string using enumerable
Front
str.each_char {|char| <what to do>}
str.each_char.with_index {|char,idx| <what to do>}
str.each_char do |char|
<what to do>
end
str.each_char.with_index {|char,idx| <what to do>}
str.each_char.with_index do |char,idx|
<what to do>
end
Back
hsh.each
Front
enumerates over the hash, passing in key and value
Back
Hash.new()
Front
creates a new hash with a default value specified in the parentheses
default value commonly set to 0 if you want to create a counter hash
Back
hash
Front
a datatype that consists of key=>value pairs
useful for describing many facets of a given thing
Back
nested loop
Front
a loop inside the body of another loop
Back
hsh.each_value
Front
enumerates over the hash, passing in value
Back
sorting a hash
Front
for a hash hsh:
-note that sort by takes in key as its first param, value as its second
hsh.sort_by{|k,v| v} if you want to sort by values
hsh.sort_by{|k,v,| k} if you want to sort by keys
-chars will be sorted alphabetically
-values will be sorted low to high
Back
hsh.each_key
Front
enumerates over the hash, passing in key
Back
iterate a number of times
Front
num.times{<what to do>}
Back
nil
Front
the default value in a hash, represents "nothing" or "empty"
Back
hsh["key"], hsh["key"] = value
Front
calls value of key in hsh, assigns value of key in hsh to value