w1d1 Flashcards
What is a method?
A block of code referred to by name.
What’s the difference between a method and a function?
A method is associated with an object.
A function stands alone.
Does ruby have functions? Why or why not?
Not strictly. All functions in ruby are actually methods as they are all associated with an object.
In regards to a method, what is meant by its “side effects” ? What is a commonly used function with a side effect?
A side effect is some sort of modification to a non-local variable or some sort of observable interaction with the outside world.
Example: puts
What is an implicit return? Is this preferred? Why or why not?
The last evaluated expression in a method is the return value of that method.
What are three signs of a good method?
It does one thing.
It has a short description and a good name.
It’s body is under 10 lines long.
How many lines should a method be limited to?
10
What is an object?
A convenient way of grouping data and behavior.
Data and behavior is grouped together within a..
object
What in ruby is not an object?
Trick question: everything is an object.
What is the parent class for all objects in Ruby?
Object
Describe Object’s relation to other classes in Ruby.
Object is the parent class for all other classes.
What’s the difference between calling puts and p on an object?
puts returns object.to_s
p returns object.inspect
Why does nil.nil? return true?
Object#nil?
returns false, while only NilClass#nil? returns true
How do you test to see if an object is an instance of a particular class?
Object#is_a?
What module is mixed into every class?
Kernel
Why are methods like puts and gets available to any context?
The Kernel module is mixed into every class.
Array#each is a bad way to delete elements within an array. What’s a better method?
Array#delete_if
How do we tell if an array has 0 elements?
Array#empty?
What two methods are used when treating an Array as a Queue?
Array#push
Array#shift
What’s a naming technique that helps the user known that a variable is an array?
Make it plural.
Why is an array variable name typically pluralized?
To help the user know that the variable represents an array.
Why not mix types in an array?
It makes it difficult to iterate when the type of the item changes.
Why is string interpolation preferred over concatenation?
It’s easier to read.
str = “asdfjkl”
What’s a way to access the substring “dfj” of str using only the bracket operator?
str[2,3]
How do you replace all instances of a pattern within a string?
String#gsub
How do you set up multiple replacements within a single call to String#gsub?
pass it a hash where the keys are the matches and the values are the replacements.
How do you convert a semi-string, i.e. “123 Flatbush” into an integer? What will it return in this example?
String#to_i
123
What’s the purpose of String#to_sym?
Return a symbol that points to the string.
What does nil.to_s return?
”” (empty string)
What’s the difference between:
bundle exec rspec
rspec
The first changes PATH to the local directory. This causes the local RSpec version to be used.
The second uses the system’s version of RSpec, which may be out of date or incompatible with the spec’s.
Rule of thumb: how much longer does it take to debug than to write code?
10x
What’s the term for an input that produces the wrong output?
a regression
How do you step into a method call in byebug?
step
How do you step out of a method call in byebug?
finish
What are two cases where a NameError might be thrown? What are their error messages? When are they each thrown?
NameError: undefined local variable – thrown when using a variable or method that hasn’t been defined
NameError: uninitialized constant – thrown when a class or other constant is referenced but cannot be found.
when using a variable or method that hasn’t been defined, what Exception will be thrown, and with what error message?
NameError: undefined local variable
when using a Class that can’t be found by the interpreter, what Exception will be thrown, and with what error message?
NameError: uninitialized constant
When might a NoMethodError be thrown?
when it’s clear that the user tried to call a method (not a variable) that doesn’t exist.
when it’s clear that the user tried to call a method (not a variable) that doesn’t exist., what error is thrown?
NoMethodError
If we don’t give a method the right number of arguments, what exception will be thrown at us? What error message?
ArgumentError: wrong number of arguments
When might a TypeError be thrown? What’s a typical error message?
if you pass the wrong type of thing to a method.
TypeError: String can’t be coerced into Fixnum
if you pass the wrong type of thing to a method, what error is thrown? What error message?
TypeError: can’t be coerced into
What are two cases where a LoadError might be thrown? What’s a typical error message?
LoadError: cannot load such file –
This can happen when you’re requiring a gem that hasn’t been installed.
This can also happen when you’re trying to require a file without using the initial “./”
What error is thrown when you’re requiring a gem that hasn’t been installed?
LoadError
What error is thrown when you’re trying to require a file without using the initial “./”
LoadError
Writing ungrammatical code will throw what type of error?
SyntaxError
What are 3 common causes of SyntaxErrors?
forgetting to close quotes
forgetting to close parentheses
forgetting to close do-end blocks
What caused this error:
SyntaxError: /Users/ruggeri/test.rb:4: syntax error, unexpected keyword_end, expecting $end
An extra end statement
What caused this error:
SyntaxError: /Users/ruggeri/test.rb:3: syntax error, unexpected $end, expecting keyword_end
A missing end statement
An extra end statement will throw what error?
SyntaxError: /Users/ruggeri/test.rb:4: syntax error, unexpected keyword_end, expecting $end
A missing end statement will throw what error:
SyntaxError: /Users/ruggeri/test.rb:3: syntax error, unexpected $end, expecting keyword_end
How do you call a block with a single parameter ‘name’ from a method?
within the method, use:
yield(name)