Vanilla Flashcards

You may prefer our related Brainscape-certified flashcards:
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
Q

what is the inverse of #reject?

A

#select

26
Q

what does the gets method stand for and do

A

GET String, waits for user input

27
Q

what is another name for attribute?

A

instance variable

28
Q

what is ARGV

A

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
Q

in what scenario would we definetly want to use Array.each to iterate rather than for item in array?

A

when we want to also receive the iterations index

30
Q

in ruby what is truthy and what is falsey?

A

literallyt everything that is no false or nil is truthy, even the integer 0 is truthy

31
Q

what is a String class method that can replace all occurances of one string with another?

A

.gsub (as in Global Substitute)

e. g.
poem. gsub(“toast”, “honey”)

32
Q

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

A
33
Q

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

A

to_i

34
Q

how do we run a command in the terminal using ruby

A

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
Q

before doing anything with an any enumerable, what should be the first step?

A

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
Q

how do we allow an instance method to be chainable?

A

by using

return self

37
Q

what is the result of “some string”.to_i ?

A

0

38
Q

how can we pass in and use an operator in a method?

A

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
Q

how do you do less than and greater then in a case statement?

A

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
Q

ruby naming convention?

A

name_variables_with_snake_case

ClassesUseCapitalCamelCase

41
Q

what does the .each method return?

A

it returns the original array, it does not return a modified array or anything at all in the block statement

42
Q

how to print a block of text in ruby?

A

print “””
somet text
some more
blah blah bhlah
“””

43
Q

map_with_index doesn’t exist as a core method, so what is the easy work around?

A

map.with_index

44
Q

hash keys can be defined in what way to save memory?

A

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
Q

what do you call the equivalent of a dictionary in Ruby?

A

a Hash

46
Q

difference between String.chomp and String.strip?

A

chomp gets rid of whitespace at the end, strip gets rid of leading and trailing white space

47
Q

do ruby methods need a return statement?

A

no. they will automatically return the last statement in the method if no return is provided

48
Q

How do we represent infinity in ruby?

A

-Float::INFINITY

and

Float::INFINITY

49
Q

what class does every class created by Ruby or a user inherit from?

A

the Object class

50
Q

what is a deep copy and how can we avoid them if desired?

A

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
Q

difference between p, puts, print

A

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
Q

how do we make a class variable and how does it work?

A

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
Q

what is a Hash in ruby?

A

it is like a dictionary

54
Q

what do methods with a ! or an ? at the end do?

A

? 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
Q

when doing a begin and rescue, what is the difference between using p and puts for the error?

A

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
Q

what expression can you use as a shortcut for .push on an Array?

A

<<

57
Q

inside #{} what can be written?

A

any ruby code at all, it will be executed and whatever it returns is printed as a string where the #{} was

58
Q

what is the inverse of #select?

A

#reject

59
Q

How do you work with the Hash#each block?

A

meals = {
breakfast: “oats”,
lunch: “curry”,
dinner: “ramen”
}

meals.each do |key, value|
puts value
end

60
Q

how can you do an each over just a range of indexes?

A

just put a range in the index like so:

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
Q

what does This::Syntax mean?

e. g.
* robot = ToyRobot::Robot.new*

A

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

  • . . .*
  • . . .*
  • . . .*