Vanilla Flashcards
what native class method can you call to convert the object to a float?
.to_f
what type of quotes can be used for string interpolation ?
only double quotes, normally double or single can be used to create a string
describe and name all the methods to add and remove elements to the beginning and end of an Array
push/pop will add/remove from the end of an Array
shift/unshift will remove/add from the beginning of an Array
what Array method can we use to remove duplicates?
Array.uniq
what native class method can you call to convert the object to a string?
to_s
e.g. [1] pry(main)> 40.to_s => “40” [2] pry(main)> 40 => 40
how do we create or use a range in Ruby?
(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
can you access a variable created in a while loop from outside that loop?
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 create blocks of comments?
=begin
kjsanfafdjsankfnkdjkjndfsa
=end
how can we create a method that mutates a variable? (modifies in place)
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 to remove letters from a String?
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”
what is a class constructor and how do we use it?
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 does the <=> aka spaceship operator work?
also generally what can it be useful for?
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 do we print to terminal in ruby code
puts e.g. puts 1+1
what are the rules of thumb for where to put exception handling?
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
native type method to remove trailing and leading white space?
native type method to remove only trailing white space?
String.strip
and
String.chomp
describe the gotchya when using ARGV and there is a gets in your script
gets must be changed to STDN.gets
what is another name for instance variable?
attribute
how do we make an instance variable and how does it work?
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 access an index of an array where you know the contents but not the index value?
four_letter_animals[four_letter_animals.index(“elephant”)]
what is an easy way in ruby to create a break point ?
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 does Array.pop work?
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
describe how we can use instance variables
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
can you string together ruby methods?
yes
e.g.
some_string.reverse.downcase
what Array method can we use to concatonate an array of strings and create a single string?
.join
e.g.
array_of_strings.join