Vanilla Flashcards

(61 cards)

1
Q

what native class method can you call to convert the object to a float?

A

.to_f

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

what type of quotes can be used for string interpolation ?

A

only double quotes, normally double or single can be used to create a string

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

describe and name all the methods to add and remove elements to the beginning and end of an Array

A

push/pop will add/remove from the end of an Array

shift/unshift will remove/add from the beginning of an Array

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

what Array method can we use to remove duplicates?

A

Array.uniq

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

what native class method can you call to convert the object to a string?

A

to_s

e.g. [1] pry(main)> 40.to_s => “40” [2] pry(main)> 40 => 40

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

how do we create or use a range in Ruby?

A

(1..5) or (1…5) create a range that can be iterated over in a for loop with the items 1 to 5 inclusive and 1 to 4 inclusive respectively

we can also create an array out of them by doing (1..5).to_a

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

can you access a variable created in a while loop from outside that loop?

A

no, it must be declared before going into the while loop if you want to do so. otherwise it is confined to the scope of that while loop

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

how create blocks of comments?

A

=begin

kjsanfafdjsankfnkdjkjndfsa

=end

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

how can we create a method that mutates a variable? (modifies in place)

A

with the .replace method. this will only work on string and arrays though, integers for example are too primative to be worked with in this way as they don’t store state (note the bang! isn’t required, but probably good practice

e.g.

def lev_pop!(array)
new_array = []
removed = array[-1]
array.each_with_index {
|element, i|
if i != array.length - 1
new_array.push(element)
end
}
array.replace(new_array)
return removed
end

array = (“a”..”f”).to_a
popped_elements = lev_pop!(array)
popped_array = array
p popped_elements
p popped_array

terminal:
“f”
[“a”, “b”, “c”, “d”, “e”]

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

how to remove letters from a String?

A

String.slice, the argument is the index for the letter (if it was an array), can use ranges and negatives as well. it will return what was removed. slice! will also modify the String in place, leaving the “left over” letters

[248] pry(main)> test = “string”
=> “string”
[249] pry(main)> test.slice(0)
=> “s”
[250] pry(main)> test.slice(1)
=> “t”
[251] pry(main)> test.slice(1..3)
=> “tri”
[252] pry(main)> test.slice(-1)
=> “g”
[253] pry(main)> test.slice(-2..-1)
=> “ng”

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

what is a class constructor and how do we use it?

A

a class contructor is used to instantiate a class into and object.

when we (or Ruby) define the class, there is a constructor method that must be called initialize, initialize is a reserved word. in here we define attributes and any arguments required to instantiate.

in order to create an object of class Foobar, we write:
some\_object = Foobar.new()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how does the <=> aka spaceship operator work?

also generally what can it be useful for?

A

a <=> b :=
if a < b then return -1

if a = b then return 0

if a > b then return 1

if a and b are not comparable then return nil

it is useful for sorting an array

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

how do we print to terminal in ruby code

A

puts e.g. puts 1+1

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

what are the rules of thumb for where to put exception handling?

A

rule of thumb for where to put exception handling:

  • anywhere that requires user input where they can input something that your code cannot accept or support or do anything with
  • anything that uses some sort of database where a value or the class of the value could potentially be something unsupported or left blank etc
  • anything using APIs to return results and anything using those results
  • anything attempting internet/network communication
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

native type method to remove trailing and leading white space?

native type method to remove only trailing white space?

A

String.strip

and

String.chomp

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

describe the gotchya when using ARGV and there is a gets in your script

A

gets must be changed to STDN.gets

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

what is another name for instance variable?

A

attribute

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

how do we make an instance variable and how does it work?

A

putting an @ infront of the variable and it is accessable instance wide (each of object of that calss has it’s own independent @variable

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

how access an index of an array where you know the contents but not the index value?

A

four_letter_animals[four_letter_animals.index(“elephant”)]

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

what is an easy way in ruby to create a break point ?

A

add the line binding.pry (must require ‘pry’) first

you can check what values a variable is at this break point by simply typing in it’s name

e.g.

require ‘pry’

x = 0
until x == 5
p x
x += 1
binding.pry
y = 0
end

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

how does Array.pop work?

A

pop with remove the last (or more with an integer argument) element of an array in place.

it will also return all the popped elements in an array

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

describe how we can use instance variables

A

an instance variable has class scope

it cannot be used as a paremeter for a method in the class, but it can be passed as an argument to the method

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

can you string together ruby methods?

A

yes

e.g.

some_string.reverse.downcase

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

what Array method can we use to concatonate an array of strings and create a single string?

A

.join

e.g.

array_of_strings.join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
what is the inverse of *#reject*?
*#select*
26
what does the gets method stand for and do
GET String, waits for user input
27
what is another name for attribute?
instance variable
28
what is ARGV
it is an argument that can be given at runtime via the command line running the script. it can be accessed with ARGV[0], ARGV[1], etc e.g. ruby some\_script -t where in some\_script, there is a conditional somewhere that executes if ARGV[0] == "-t"
29
in what scenario would we definetly want to use *Array.each* to iterate rather than *for item in array*?
when we want to also receive the iterations index
30
in ruby what is truthy and what is falsey?
literallyt everything that is no false or nil is truthy, even the integer 0 is truthy
31
what is a String class method that can replace all occurances of one string with another?
.gsub (as in Global Substitute) e. g. poem. gsub("toast", "honey")
32
what native class method can you call to convert the object to a array?
33
what native class method can you call to convert the object to a integer?
to\_i
34
how do we run a command in the terminal using ruby
em { background-color: lightgray; padding: 10px; border-radius: 10px; } put the command in a backtick e.g. *`touch example.txt`* or *`touch #{var_name}.txt`*
35
before doing anything with an any enumerable, what should be the first step?
go to the documentation for enumerable and then the type of enumerable, e.g. array, range, hash, etc and see if it can be done via one of those methods first.
36
how do we allow an instance method to be chainable?
by using return self
37
what is the result of "some string".to\_i ?
0
38
how can we pass in and use an operator in a method?
we can use symbols and the .send method e.g. def arithmetic(num0, num1, operator) result = num0.to\_f.send(operator, num1.to\_f) return result end p arithmetic(1, 2, :+) p arithmetic(4, 2, "-") p arithmetic(6, 4, :\*) p arithmetic(7, 3, "/")
39
how do you do less than and greater then in a case statement?
you can't, you have to use a range e.g. case distance when (0...10) puts "long way to go but you're doing amazing!" when (10...20) puts "almost halfway now, killing it!" when (20...30) puts "now for the final push, keep going!" when (30...40) puts "sprint!!!!" end
40
ruby naming convention?
name\_variables\_with\_snake\_case ClassesUseCapitalCamelCase
41
what does the .each method return?
it returns the original array, it does not return a modified array or anything at all in the block statement
42
how to print a block of text in ruby?
print """ somet text some more blah blah bhlah """
43
map\_with\_index doesn't exist as a core method, so what is the easy work around?
*map.with\_index*
44
hash keys can be defined in what way to save memory?
using symbols, like so some\_hash = { symb: value } reference\_a\_hash = some\_hash[:symb] as opposed to some\_hash = { "string\_key" =\> value 23 =\> value } reference\_a\_hash = some\_hash["string\_key"] reference\_a\_hash = some\_hash[23]
45
what do you call the equivalent of a dictionary in Ruby?
a Hash
46
difference between String.chomp and String.strip?
chomp gets rid of whitespace at the end, strip gets rid of leading and trailing white space
47
do ruby methods need a return statement?
no. they will automatically return the last statement in the method if no return is provided
48
How do we represent infinity in ruby?
*-Float::INFINITY* and *Float::INFINITY*
49
what class does every class created by Ruby or a user inherit from?
the Object class
50
what is a deep copy and how can we avoid them if desired?
a deep copy is when one variable is assigned to another, and then modifying one of those variables also modifies the other. most common example is with arrays [114] pry(main)\> a = (0...10).to\_a =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [115] pry(main)\> b = a =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [116] pry(main)\> a.pop =\> 9 [117] pry(main)\> a =\> [0, 1, 2, 3, 4, 5, 6, 7, 8] [118] pry(main)\> b =\> [0, 1, 2, 3, 4, 5, 6, 7, 8] we can avoid this with the .dup method: [119] pry(main)\> a = (0...10).to\_a =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [120] pry(main)\> b = a.dup =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [121] pry(main)\> b =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [122] pry(main)\> a.pop =\> 9 [123] pry(main)\> a =\> [0, 1, 2, 3, 4, 5, 6, 7, 8] [124] pry(main)\> b =\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [125] pry(main)\>
51
difference between p, puts, print
p and puts includes a new line character, print does not. p prints the "quotes", good for debugging so it is clear if something is a string or not (example an integer)
52
how do we make a class variable and how does it work?
using @@ syntax this variable is shared across all instances of the class, the value is the same across all sintances and changing the value in on instance changes it in all the others
53
what is a Hash in ruby?
it is like a dictionary
54
what do methods with a ! or an ? at the end do?
? simply indicates that a true or false will be returned ! indicates that this is a "dangerous method", basically it will modify the object it is called on, instead of simply outputing a result of the method being called on the object
55
when doing a begin and rescue, what is the difference between using p and puts for the error?
puts will just give the contents of the raise statement as defined by the script p will give you the full error type and message generated by ruby
56
what expression can you use as a shortcut for .push on an Array?
\<\<
57
inside #{} what can be written?
any ruby code at all, it will be executed and whatever it returns is printed as a string where the #{} was
58
what is the inverse of *#select?*
*#reject*
59
How do you work with the *Hash#each* block?
*meals = { breakfast: "oats", lunch: "curry", dinner: "ramen" }* *meals.each do |key, value| puts value end*
60
how can you do an each over just a range of indexes?
just put a range in the index like so: ## Footnote *start\_index = train\_line.stops.find\_index { |stop| stop[:name] == station\_start } finish\_index = train\_line.stops.find\_index { |stop| stop[:name] == station\_finish } stops = [] train\_line.stops[start\_index..finish\_index].each do |stop| stops \<\< stop[:name] end*
61
what does *This::Syntax* mean? e. g. * robot = ToyRobot::Robot.new*
it the Syntax class from the This module e.g. ``` *module ToyRobot class Robot attr\_reader :north, :east, :facing* ``` *def initialize(north = 0, east = 0, facing = "north") @north = north @east = east @facing = facing end* * . . .* * . . .* * . . .*