Ruby Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

An edge case is…

A

something outside of the expected normal input

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

Give an example of string interpolation

A

name = “tina”
puts “my name is #{name}”
=>my name is tina

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

How do we get a hash value?

A

furniture[“name”]

=>”bed”

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

Most straight forward way of creating a hash

A

furniture = {“name” => “bed”, “colour” => “brown”, “material” => “wood”}

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

How do we add or change data in a hash?

A

to add new key and value:
furniture[“size”] = “king size”

to change value is same approach:
furniture[“size”] = “double”

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

How do we define a method in ruby?

A
def method()
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what does ‘gets’ do?

A

get string waits for user to make a keyboard input and passes it to the program

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

what does .chomp do?

A

removes the carriage return characters at the end of a string in such cases as after using ‘gets’

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

Constants are declared by…

A

Capitalizing every letter

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

Global variables are declared using which symbol?

A

$variable #usually avoided in ruby as can cause problems

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

Class variables are declared using which symbols?

A

@@class_variable

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

Class variables must be initialized at…

A

Class level, outside of method definitions

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

Instance variables are declared using which symbol?

A

@instance_variable

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

How do we type ‘else if’ in Ruby?

A

elsif

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

Opposite of if?

A

unless

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

Short hand for if else

A

? :

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

Loop takes a block denoted by..

A

{ } or do….end and loops it.

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

Name 2 ways of ending a loop

A

Break and Return

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

To skip an iteration in a loop we can use..

A

next

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

The opposite of a while loop is an

A

until loop

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

what does range do?

A

captures a range of elements i.e
1..5
=> 1,2,3,4,5

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

Methods that loop over a given set of data, allowing you to operate within each element in the collection

A

Iterators

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

Lines of code that are ready to be executed, contained within { } or do and end are called

A

Blocks

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

Recursion is the act of…

A

calling a method from within itself

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

.first

A

finds the first element from an array

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

.last

A

finds the last element in an array

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

.pop

A

removes the last element from an array and returns it

(this method mutates the caller)`

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

what is the shortcut for the push method?

A

array«

also known as shovel

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

.push

A

adds element to the end of an array

mutates the caller

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

.map and .collect

A

iterate over a collection applying a block to each element in the collection, then returning a new collection.

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

.delete_at

A

takes an array index and deletes the corresponding array item without leaving a gap

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

.delete

A

takes a value and deletes the corresponding array item(s)

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

.uniq

A

finds duplicates within an array and returns a new array with duplicates removed

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

.select

A

returns all elements in a collection that return true to the conditions that you specify

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

What does the bang operator (!) after a method usually signify?

A

that it will mutate the caller

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

.unshift

A

adds a specified value to the start of an array

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

.include?

A

checks if an argument given is included in the array

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

? after a method usually indicates?

A

that it will return a boolean value.

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

.flatten

A

converts a nested array into a normal array

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

.eachindex

A

passes the index of an array element rather than a value into the block

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

.each_with_index

A

passes both index and value of array element to the block

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

.sort

A

returns a sorted array

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

What is the difference between .each and .map?`

A

each discards the return value making no changes whereas map creates a new array from its return value

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

What is a class?

A

Basically a custom data type that you can create

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

What is the setter/getter syntax for classes?

A

attr-accessor

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

Putting a : before anything defines it as what?

A

Symbol

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

Multi line comment

A

=begin

=end

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

What’s the difference between anIntegerand aFloat?

A

Integer is a whole number, float has a decimal

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

What’s the difference between==, and===?

A

== is a comparison operator, whereas === typically asks whether the thing on the right is of the same type or is a member of the thing on the left.

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

How do you do exponents in Ruby?

A

x**y (x to the power of y)

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

What’s the difference between(1..3)and(1…3)?

A

(1..3) includes 1 and 3 whereas (1…3) excludes 3

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

What are three ways to create a range?

A

1..10
1…11
Range.new(start, finish)

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

What’s the difference between single and double quotes?

A

Nothing apart from you should only use string interpolation inside double quotes and if you need to use quotes within quotes then use single inside doubles.

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

What are escape characters?

A

Escape characters () waiver characters and meta characters from acting how they would normally, escaping their function I.e “my name is #{name}” => “my name is #{name}”

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

What are line breaks?

A

code used to create a new line in the terminal i.e (\n)

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

How do you make other things into strings?

A

to_s

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

How do you concatenate strings?

A

“like ” + “ this”

“like”.+(“this”)

58
Q

How do you access a specific character or substring?

A

By treating it like an array: s = “hello”
s[0]
=> “h”

s[-1]
=> “o”

s[1..-2]
=> “ell”

59
Q

How do you split up strings into arrays?

A

split()

60
Q

How do you get and clean up user input on the command line?

A

gets.chomp

61
Q

What does it mean that strings are “mutable” and why care?

A

Mutable objects can be added to, reversed and messed around with in 100 different ways. Immutable objects such as symbols cannot be changed, which is why they make good hash keys.

62
Q

What is a symbol?

A

A symbol is like a string but with no depth. It is immutable so it cannot be changed, and is perfect for use as names that don’t need to change such as in hash keys. It is denoted as follows :symbol

63
Q

What is a Regular Expression (RegEx)?

A

Regex is a special syntax used to find things

64
Q

How can you center or right-justify a string?

A

center() and .rjust()

65
Q

What are three ways to create an array?

A
a = Array.new
b = []
c = Array[]
66
Q

How do you pre-populate an array with default data?

A

full_array = Array.new(3, “hi”) => [“hi”, “hi”, “hi”]

67
Q

How can you access a specific group of items in an array?

A

array = [1, 3, 5, 7, 2]

array[0] => 1, array[-1] => 2, array[1..3] => [3,5,7]

68
Q

How do you modify the items in an array?

A

Same as accessing them, except an assignment operator is used: array[0] = 2

69
Q

How do you combine arrays?

A

Similar to strings, you use the + operator

70
Q

How do you find the values in one array that aren’t in another?

A

by using subtract

[1,6,3,4] - [1,6,3,5] => [4] (note the 5 is not returned because it is on the right hand side)

71
Q

How do you find duplicate values in 2 arrays?

A

by using ampersand

[1,2,3] & [2,4,5] => [2]

72
Q

What is the difference betweenpush/popandshift/unshift?

A

push and pop deal with the end of an array; adding and removing the last element, whereas shift and unshift deal with the beginning of an array; adding and removing the first element.

73
Q

How is arr.popdifferent from arr[-1]?

A

Arr.pop removes the last item of an array before returning it, whereas arr[-1] just returns it.

74
Q

How ispushing or&laquo_space;ing another array into your array different from just adding them together?

A

Pushing an array into your array will create a nested array whereas adding an array will just add to it.

75
Q

Why should you be careful deleting items in an array?

A

Because it will change the index of all the other items after it, which must be considered for things like loops etc.

76
Q

How can you convert arrays to strings?

A

join()

77
Q

How can you convert from other data types to arrays?

A

to_a

78
Q

How do you find the biggest item in an array?

A

max

79
Q

How do you find the smallest item in an array?

A

min

80
Q

How do you find out how big an array is?

A

size

81
Q

How do you put an array in order?

A

sort

82
Q

What are the naming conventions for arrays?

A

Always use plurals and be descriptive

83
Q

What is a hash?

A

A container for data, like arrays, but instead of storing data based on numeric indices, you use “keys” which can be strings or symbols. This makes hashes more appropriate for storing data with a bit more depth to it.

84
Q

What are keys and values?

A

keys are a named address of a corresponding value that is stored, i.e. key: “name” value: “john”

85
Q

How is a hash different from an array?

A

They are different in that a hash is not stored in any order and hashes are declared with curly braces instead of square brackets.

86
Q

What are 2 ways to create a hash?

A
my_hash = Hash.new
my_hash = {},
87
Q

What are options hashes?

A

they are parameters passed to a method in a hash

88
Q

How do you delete data from a hash?

A

Deletefrom a hash by just setting the value tonilor by using thedeletemethod

89
Q

How do you add hashes together?

A

hash1.merge(hash2)

90
Q

How do you list out all the keys or values?

A

Use the .keys or .values method

91
Q

How do you see if the hash contains a key or value?

A

has_key?() has_value?()

92
Q

What is a set?

A

A simpler kind of hash is called aset, and it’s just a hash where all the values are either True or False. It’s useful because your computer can search more quickly through this than an array

93
Q

How do you get the current date and time?

A

by creating a time object – Time.new or Time.now

94
Q

How do you find just the Year? Month? Hour? Second? Weekday?

A

by using .year, .month, .hour, .second, or .wday method on a time object

95
Q

How do you create aTimespecifically for 12/25/2013?

A

Time.new(year, month, day, hour, min, sec, time_zone_offset_from_utc)

96
Q

How do you find how many days have passed between twoTime’s?

A

by simply subtracting one from another

97
Q

How would you find out the time that was 100 seconds ago? 10 days ago?

A

By subtracting the seconds i.e.

Time.now - 100

98
Q

What isnil?

A

nil represents literally nothing and is the absence of value

99
Q

How do you check if something isnil?

A

nil?

100
Q

What’s the difference betweenputsandpandprint?

A

Print runs the .to_s method whilst puts also does this but adds a new line after it. P runs the .inspect method which returns more information than print and puts.

101
Q

What doesinspectdo?

A

Returns a string containing a human-readable representation ofobj. The defaultinspect shows the object’s class name, an encoding of the object id, and a list of the instance variables and their values (by callinginspecton each of them).

102
Q

What do+=,-=,*=and/=do?

A

a += bis the same asa = a + b
a -= bis the same asa = a – b
a *= bis the same asa = a * b
a /= bis the same asa = a / b

103
Q

What is parallel assignment?

A
is when you assign the values of more than one variable at a time i.e:
a, b = 1, "hi"
a 
=> 1
b
=>
“hi”
104
Q

What’s the easiest way to swap two variables?

A

By using parallel assignment i.e.

a,b = b,a

105
Q

What does the gsub method do?

A

gsub(pattern, replacement)

global substitute, a string method that takes a pattern such as regex or a string in the 1st parameter, finds it and replaces it with the 2nd parameter

106
Q

What is a “boolean”?

A

A data type with only 2 possible values; true and false

107
Q

What are “truthy” values?

A

In Ruby, it’s simple: nil and false are false and that’s it. Everything else is “truthy”.

108
Q

What does <=> do?

A

The ‘spaceship’ operator gives three different possible outputs (1, 0 or -1) depending on whether the left side is greater than, less than, or equal to the right side.

109
Q

What is a “boolean”?

A

A value of either true or false

110
Q

What are “truthy” values?

A

Anything that is not nil or false is a truthy value

111
Q

Are nil, 0, “0”, “”, 1, [], {} and -1 considered true or false?

A

All true except for nil.

112
Q

What do || and && and ! do?

A

Or, And, Not respectively.

113
Q

What is ||=?

A

it basically expands to thing_a || thing_a = thing_b. So if thing_a hasn’t been set to anything, it becomes thing_b, otherwise it keeps its original value.

114
Q

What is the ternary operator?

A

condition ? do_this_if_true : do_this_if_false

115
Q

When should you use a case statement?

A

For situations where you’re just checking to see if something equals any one of a number of clear but different options, a case statement can be a good substitute instead of If.

116
Q

What is an index variable?

A

This is used in a loop to keep track of which iteration you are on or to otherwise increment until a condition is reached

117
Q

How do you stop a loop?

A

break

118
Q

How do you skip to the next iteration of a loop?

A

next

119
Q

How would you start a loop over again?

A

redo will let you restart the loop (without evaluating the condition on the first go-through), usually with some condition

120
Q

What does nesting loops mean and when would you use it?

A

Nesting loops occurs when one goes inside another, so you execute the entire inner loop for each iteration of the outer loop. Often used for “two-dimensional” problems, like searching through arrays within arrays.

121
Q

How is a block like a function?

A

Blocks are often called “anonymous functions” because they have no name but behave much like functions. They’re like little helper functions… you don’t find blocks just hanging around without some method (like #each) using them.

122
Q

What are the two ways to declare a block?

A

{ } or do…end

123
Q

How do you return data from a block?

A

Implicitly. So whatever is on the last line of the block is returned. You cannot use ‘return’.

124
Q

What happens if you include a return statement in a block?

A

This will return you from whatever method actually called the block, which is bad.

125
Q

What does yield do?

A

Yield allows a method to run a block from outside the method

126
Q

How do you pass arguments to a block from within a method?

A

yield(argument)

127
Q

How do you check whether a block was actually passed in?

A

block_given?, or rather: yield if block_given?

128
Q

What is a proc?

A

A Proc is just a block that you save to a variable, thereby giving it a bit more permanence

129
Q

What’s the difference between a proc and a block?

A

Procs are objects, blocks are not. You can pass multiple procs to methods, whereas you can only pass one block to a method.

130
Q

What is a closure?

A

A closure is a function that: 1. can be passed around as a variable and 2. binds to the same scope in which it was created; Similar to a suitcase, it’s a group of code that when opened (i.e. called), contains whatever was in it when you packed it (i.e. created it).

131
Q

What is a lambda?

A

A lambda is similar to a proc except it checks the number of arguments it is passed is correct.

Lambdas will only return from the lambda itself and not the enclosing method, which is what happens if you use return inside a block or Proc.

132
Q

What is a Method (capital “M”)?

A

Method’s (capitalized because they’re actually a class of their own) are really just a convenient way to pass a normal method to another normal method by wrapping the symbol of its name in the word method()

133
Q

What is a module?

A

Ruby Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions.

134
Q

Why are modules useful?

A

they allow you to share functionality between classes - if a class mixes in a module, that module’s instance methods become available as if they had been defined in the class.

135
Q

What does inject do?

A

inject or reduce passes not just the element but also whatever was returned by the previous iteration into the block.

136
Q

How do you check if every item in a hash fulfills a certain criteria?

A

all?

137
Q

What (basically) is an enumerator?

A

A class which allows both internal and external iteration.

138
Q

How many things should a method ideally do?

A

One. If it’s doing two, it’s time for another method. If it’s doing a dozen, you probably need to start thinking about having a separate class.

139
Q

What does self mean?

A

it refers to whatever object the current method was called on (the “caller”). So if I called current_user.jump, current_user is the caller of that method. Inside the definition of the #jump method, self would refer to the current_user.

140
Q

What do you need to do to create your own Ruby script file?

A

Just save your file with a .rb extension