Ruby Flashcards
What is an index?
Each item in an array. It begins and 0 and continues counting in increments of 1.
In this array what position does the number 400 hold?
[100, 200, 300, 400, 500]
3
What does // array[2] // mean?
It is asking for the 3rd index item in an array.
What are arrays of arrays called?
Multidimensional arrays
A built in module for mathematics
Math
One role of a module is to…
group together similar methods under one familiar name
a period or dot is used to…
identify the receiver of a message
def
how we DEFINE a method
end
tells Ruby we are done with whatever we were defining
{ }
Ruby’s way of inserting something into a string
==
is equal to
!=
is not equal to
.include?
evaluates to true if it finds what it’s looking for and false otherwise
.gsub!
global substitution - replaces every instance of something with something else
+=, -=, *=, and /=
increment or decrement by a value
iterator
An iterator is just a Ruby method that repeatedly invokes a block of code. The code block is just the bit that contains the instructions to be repeated, and those instructions can be just about anything you like!
number.times
quick way to do something over and over…
10.times { print “Chunky bacon!” }
.split()
takes a string and returns it as an array separated by what ever you give it (commas, spaces, etc.)
multidimensional arrays
arrays within arrays
Histogram
A visual representation of data.
It returns 0 if the first operand (item to be compared) equals the second, 1 if first operand is greater than the second, and -1 if the first operand is less than the second.
|=
conditional operator - can use to assign a variable ONLY if it has not already been assigned.
implicit return
ruby will return the LAST evaluated expression whether or not you specifically ask it to “return” something to you
&&
returns true when both sides are true
||
returns true when one or other or both sides are true
n.times { }
can specify to do the thing inside the block “n” number of times
.each
iterates through a collection and looks at each item
.even?
checks if number is even evaluates to true
.upto
to print out an ascending range of values
Example: 95.upto(100) {|num| print num, “ “}
letters too!!
.downto
to print out a descending range of values
95.downto(85) {|num| print num, “ “}
letters too!
.respond_to?
takes a symbol and returns true if an object CAN receive that method and false otherwise
Example: [1, 2, 3].respond_to?(:push)
.push OR
adds a value to the end of an array OR to the end of a string
.to_s?
changes item to a string
string interpolation
#{ } Example: drink = coffee I love #{drink} in the mornings.
one line if statement for:
if one is less than two puts “something”
if 1
x**2
returns the square of a number
refactoring
improving the code’s structure, appearance and performance without modifying overall behavior
do one line “if” and “unless” statements need “ends”?
No
give a definition of a ruby block
a bit of code to be executed
name the two ways that ruby blocks start and end
curly braces { } OR do..end
blocks CAN be combined with methods like .each and .times to …
complete an action for each element in a collection
.collect
takes a block and applies it to every element in an array Example: my_nums = [1,2,3] my_nums.collect {|num| num**2} ==> [1,4,9]
Fill in the blanks
def double( \_\_\_) yield(\_\_\_) puts "This is a method including a yield"
double(___) {|n| puts n*2}
n, n, 9
instead of 9 any number would do
to pass a proc to a method that normally would expect a block you must use the ____ sign in front of the saved proc
& sign
Example:
[4,5,6].map!(&cube) ==> [64, 125, 215]
.call
calls a proc
proc/lambda - name 2 differences
lambda check the number of arguments given to it, proc does not
lambda returns and passes control back to calling method/proc returns immediately without going back
command line - ls
list directories
command line - pwd
print working directory - shows you where you currently are
command line - cd
change directory
command line - cd ..
change one level up in the directory
command line - mkdir
make directory
command line - touch
creates a new file in teh working directory
command line - -a
an option that modifies the behavior of ls. tells it to also list all contents including hidden files and directories (those starting with a .)
command line - -l
option modifying ls. tells it to list all contents in long format
command line - -t
option modifying ls. tells it to order files and directories by time last modified
command line - -alt
a combination of options (a) (l) (t). multiple options can be typed together
command line - cp
copies files or directories
cp frida.txt lincoln.txt copied the contents from frida into lincoln
how to use command line cp
cp foldername/filename newfilename
AND
cp foldername/filename foldername/filename newfile name
cp * foldername/
the * selects groups of files. it is known as a wildcard. It selects all files in the working directory
command line - mv
as a mover
moves files and directories
mv filename.txt foldername/
command line -mv
as a copier
mv batman.txt spiderman.txt
renames batman to spiderman
command line - rm
removes files and directories - PERMANENT!
command line - rm -r
modifies rm command. stands for recursive. says delete directory and all child directories -PERMANENT!