w1d1 Flashcards
Explain the following code:
arry[1..6]
Returns the 2nd through 7th elements of the ‘arry’ array
What is the difference between [2..4] and [2…4]
Three periods is not inclusive of the last element; two periods is.
Explain the following code:
arry[2..-1]
Returns the third through the last element of the array.
T/F: Never modify an array while iterating through it.
T; You can get stuck in an infinite loop
What is an advantage of Array#concat?
It modifies the first array by including references to the second array; very little memory cost.
Explain Array#include? and Array#index
include? returns a Boolean for finding the argument, #index returns the index of the element as an integer or nil if it is not found.
Explain the array methods: #sort, #sort!, #shuffle, #sample
sort sorts an array, with the smallest element at index 0
T/F: It doesn’t matter if arrays are heterogeneous (contain more than one type)
F; They can, but should be homogenous whenever possible.
Y/N: Will the following code execute?
“this is my sentence”[5, 2]
Yes; when given arguments like [int, int], the first argument is the starting index of the substring and the second argument is the length of the substring. For this reason, the code returns “is”
Explain the following string methods: #strip, #chomp, #gsub
strip removes leading and trailing whitespace
When a hash is used with an enumerable, what gets passed to the block?
|key, value|
When merging hashes with #merge, what will happens when a key is shared but each hash has a different value for the key?
The hash which is being merged (the argument) into the other hash takes precedence.
What is a quick way to check if a hash has a key?
has_key?
What is the difference between Enumerable#each and Enumerable#map (also known as Enumerable#collect)? When should you use them?
each returns the original UNMODIFIED array, while #map returns a new array with the computed values.
Use #each when you only want the method’s side-effects; use #map when you want to use the returned (computed) values.
Explain Enumerable#inject
We pass inject an initial value and a block. The code block takes the current accumulator value and the next element in the Enumerable . The block is run, and the accumulator value is updated with the block’s result. When all elements have been processed, the accumulator value is returned.
nums = [1, 2, 3, 4, 5]
nums.inject(0) do |accum, element| # accum is initially set to 0, the method argument
accum + element
end
This code returns 15 (the sum of all the elements)
Explain Enumerable#select and Enumerable#count
select returns an array consisting of elements from the enumerable for which the given code block returns true.
What is the difference between Enumerable#include?, Enumerable#any?, Enumerable#all?, and Enumerable#none?
include? returns true if the argument is equal to any item in the collection.
The other 3 methods work similarly. #any? returns true if at least one element of the collection returns true given the code block, while #all? checks if all elements return true and #none? checks if all elements return false.
What is the difference between Kernel#puts and Kernel#p ?
puts calls #to_s, whereas #p calls #inspect
T/F: The following code returns true:
nil.nil?
T; this is the only case where this will happen.
What methods can be used to check the type of an object?
class and #is_a?(datatype)
What happens if you do not overload #== for a class you define?
It will always return false, unless you are comparing an object to itself.
What is “technical debt”
Code can become unwieldy when there is a rush for a deadline, so you can take out technical debt by working with “smelly” code and refactoring at another time, although remember that this is a delicate balance; debt can catch up with you!
How can we use the byebug debugger?
Add ‘require ‘byebug’’ to the top of your file, then insert the ‘debugger’ keyword where you want your breakpoint(s).
How can we add a breakpoint within byebug?
$break linenum
What is the byebug command to view the stack trace?
$wtf
What is the difference between ‘next’ and ‘step’ in byebug?
‘next’ executes the line and advances to the next line, while ‘step’ steps into the code and follows its execution. Type ‘finish’ to get out of the step.
What do ‘display’ and ‘where’ do in byebug?
‘display’ watches the value of the variable (given as an argument), and ‘where’ displays the call stack.
Explain the following error messages:
NameError
Uninitialized Constant
NoMethodError
ArgumentError
TypeError
LoadError
SyntaxError
A NameError exception is thrown when you try to use a variable or method that hasn’t been defined.
“Uninitialized constant” means that Ruby is trying to find a class (or other constant), but didn’t find it.
NoMethodError is thrown when a method or variable that does not exist/ can’t be located is called
ArgumentError is for the wrong number of arguments
A TypeError may be thrown if you pass the wrong type of thing to a method.
A LoadError occurs when a file is attempted to be loaded into a program but cannot be found.
Writing ungrammatical Ruby code will net you a SyntaxError . This tells you that Ruby didn’t understand your code.
The typical source file contains how roughly how many lines of code and how many classes?
1 class and less than 300 lines.
When using the ‘require’ keyword with our own files, what must be added to the beginning of the filename?
’./’
What is the difference between a library file and a script file?
Some Ruby source files contain definitions of classes and methods meant to be loaded by other Ruby code (called library files), while others are intended to be executed as a program (called script files).
What is the guard to add conditional execution to a file?
if $PROGRAM_NAME == __FILE__
scriptactions
end