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.