Ruby Flashcards

0
Q

What is an index?

A

Each item in an array. It begins and 0 and continues counting in increments of 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

In this array what position does the number 400 hold?

[100, 200, 300, 400, 500]

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does // array[2] // mean?

A

It is asking for the 3rd index item in an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are arrays of arrays called?

A

Multidimensional arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A built in module for mathematics

A

Math

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One role of a module is to…

A

group together similar methods under one familiar name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

a period or dot is used to…

A

identify the receiver of a message

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

def

A

how we DEFINE a method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

end

A

tells Ruby we are done with whatever we were defining

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

{ }

A

Ruby’s way of inserting something into a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

==

A

is equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

!=

A

is not equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

.include?

A

evaluates to true if it finds what it’s looking for and false otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.gsub!

A

global substitution - replaces every instance of something with something else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

+=, -=, *=, and /=

A

increment or decrement by a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

iterator

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

number.times

A

quick way to do something over and over…

10.times { print “Chunky bacon!” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

.split()

A

takes a string and returns it as an array separated by what ever you give it (commas, spaces, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

multidimensional arrays

A

arrays within arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Histogram

A

A visual representation of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

|=

A

conditional operator - can use to assign a variable ONLY if it has not already been assigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

implicit return

A

ruby will return the LAST evaluated expression whether or not you specifically ask it to “return” something to you

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

&&

A

returns true when both sides are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

||

A

returns true when one or other or both sides are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

n.times { }

A

can specify to do the thing inside the block “n” number of times

27
Q

.each

A

iterates through a collection and looks at each item

28
Q

.even?

A

checks if number is even evaluates to true

29
Q

.upto

A

to print out an ascending range of values
Example: 95.upto(100) {|num| print num, “ “}
letters too!!

30
Q

.downto

A

to print out a descending range of values
95.downto(85) {|num| print num, “ “}
letters too!

31
Q

.respond_to?

A

takes a symbol and returns true if an object CAN receive that method and false otherwise
Example: [1, 2, 3].respond_to?(:push)

32
Q

.push OR

A

adds a value to the end of an array OR to the end of a string

33
Q

.to_s?

A

changes item to a string

34
Q

string interpolation

A
#{    }
Example:
drink = coffee
I love #{drink} in the mornings.
35
Q

one line if statement for:

if one is less than two puts “something”

A

if 1

36
Q

x**2

A

returns the square of a number

37
Q

refactoring

A

improving the code’s structure, appearance and performance without modifying overall behavior

38
Q

do one line “if” and “unless” statements need “ends”?

A

No

39
Q

give a definition of a ruby block

A

a bit of code to be executed

40
Q

name the two ways that ruby blocks start and end

A

curly braces { } OR do..end

41
Q

blocks CAN be combined with methods like .each and .times to …

A

complete an action for each element in a collection

42
Q

.collect

A
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]
43
Q

Fill in the blanks

def double( \_\_\_)
yield(\_\_\_)
puts "This is a method including a yield"

double(___) {|n| puts n*2}

A

n, n, 9

instead of 9 any number would do

44
Q

to pass a proc to a method that normally would expect a block you must use the ____ sign in front of the saved proc

A

& sign
Example:
[4,5,6].map!(&cube) ==> [64, 125, 215]

45
Q

.call

A

calls a proc

46
Q

proc/lambda - name 2 differences

A

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

47
Q

command line - ls

A

list directories

48
Q

command line - pwd

A

print working directory - shows you where you currently are

49
Q

command line - cd

A

change directory

50
Q

command line - cd ..

A

change one level up in the directory

51
Q

command line - mkdir

A

make directory

52
Q

command line - touch

A

creates a new file in teh working directory

53
Q

command line - -a

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 .)

54
Q

command line - -l

A

option modifying ls. tells it to list all contents in long format

55
Q

command line - -t

A

option modifying ls. tells it to order files and directories by time last modified

56
Q

command line - -alt

A

a combination of options (a) (l) (t). multiple options can be typed together

57
Q

command line - cp

A

copies files or directories

cp frida.txt lincoln.txt copied the contents from frida into lincoln

58
Q

how to use command line cp

A

cp foldername/filename newfilename
AND
cp foldername/filename foldername/filename newfile name

59
Q

cp * foldername/

A

the * selects groups of files. it is known as a wildcard. It selects all files in the working directory

60
Q

command line - mv

as a mover

A

moves files and directories

mv filename.txt foldername/

61
Q

command line -mv

as a copier

A

mv batman.txt spiderman.txt

renames batman to spiderman

62
Q

command line - rm

A

removes files and directories - PERMANENT!

63
Q

command line - rm -r

A

modifies rm command. stands for recursive. says delete directory and all child directories -PERMANENT!