Basics Flashcards
Ruby’s class for initializing I/O streams.
IO
Method for iterating through an array and storing the results in a new array
Array#collect
ie: array.collect { |elem| elem * 2 }
Method for iterating through an array and not storing the results in a new array
Array#each
ie: array.each { |item| puts item }
String method for breaking up the string into an array at certain points
String#split
||
this operator means…
or
&&
this operator means…
and
number > 0 ? “#{number} is positive” : “#{number} is negative”
this is an example of a ________ expression
ternary expression
[1, 2, 3][2]
this method will return:
3
the 2th index
Hash method which returns an array of all the hash’s keys
Hash#keys
method which returns an object’s class
Object#class
“hello”.is_a? String
what will this return and what is the method doing?
true
the method returns a boolean on whether the object is of the argument class
For a class to justify its existence, it needs to have two distinct features:
- State
2. Behavior
A ______ is a piece of code that you can store in a variable, and is an object.
lambda
a piece of code that can’t be stored in a variable and isn’t an object.
block
_______ only hold behaviour, unlike classes, which hold both behaviour and state.
Modules
Symbol for the constant lookup operator
::
What is the scope of a local variable?
A local variable is accessible only in the method that it is defined.
What is a getter method?
A getter method is used within an object. It returns the value of a(n) instance variable(s)
What is a setter method?
A setter method is used within an object. It can alter the value of a(n) instance variable(s)
What is attr_writer?
attr_writer is a shortcut for a setter method. It allows the passed instance variable to be changed.
What is attr_reader?
attr_reader is a shortcut for a getter method/ It allows an instance variable to have its value returned.
What is a class variable and what is its notation? And how do changes to a class variable work?
Notation: @@ ie: -|@@class_variable|- It is a variable that is the same for all instances and children of the class in which the class variable is defined. A change to the class variable will be reflected in all instances/children of that class.
How accessible are class instance variables?
Class instance variables are available only in class methods
List the quick summary of RubyMonks review of class variables.
Instance variables are available only for instances of a class. They look like @name. Class variables are available to both class methods and instance methods. They look like @@name
It is almost always a bad idea to use a class variable to store state. There are only a very few valid use cases where class variables are the right choice.
Prefer class instance variables over class variables when you do really need store data at a class level. Class instance variables use the same notation as that of an instance variable. But unlike instance variables, you declare them inside the class definition directly.