Ruby basics Flashcards
How do you declare a class name?
When naming your classes you will use CamelCase formatting. CamelCase uses no spaces and capitalizes every word.
# Class naming class MyFirstClass end
class MySecondClass end
What do you use when the entire expression fits on one line in a do/end block?
Use { }
What does namespace mean and do
he :: symbol is used to define a namespace, which is just a way to group classes in Ruby and differentiate from other classes with the same name. For example ActiveRecord::Base is referring to the Base class in the ActiveRecord module, to differentiate from other classes also named Base.
However, when looking at the method list on the side bar, the :: means something different: it means that the method after the :: is a class method
What are methods denoted by :: in documentation
class methods
What are methods denoted by # in documentation
instance methods
How do we express nothing in programming
By using the value nil
How do you check if something has a nil value?
.nil?
How is nil treated in an if statement?
Treated as FALSE, as it represents an absence of content
What does => nil mean when your run puts “test”
It means that you have no return
a = puts “test”
puts a
What will be printed on the screen?
nil
What are variables
Variables are used to store information to be referenced and manipulated in a computer program.
They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.
It is helpful to think of variables as containers that hold information.
Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
Variable scope
Inner scope can access variables initialized in an outer scope, but not vice versa.
What’s the distinguishing factor for deciding whether code is a block?
arr = [1, 2, 3]
for i in arr do
a = 5 # a is initialized here
end
puts a # is it accessible here?
the key distinguishing factor for deciding whether code delimited by {} or do/end is considered a block (and thereby creates a new scope for variables), is seeing if the {} or do/end immediately follows a method invocation
Is for do..end creating an inner scope? Why is that useful?
No
Variable created in the for will be accessible outside of it as it will be in the same scope.
How many types of variables are there?
5
Constants Global variables Class variables Instance variables Local variables
Constant variables
Created with uppercased letters
Cannot be declared in a method’s definition
Can be changed in ruby, while in other languages not. Still, dont change them once declared
Global variables
created with $
are accessible everywhere in the app, overriding all scope boundaries
there can be complications when using them
Class variables
declared with @@ accessible by instances of my class and the class itself
Use when you need to create a variable related to the class and when instances of that class do not need their own value for that variable
Must be initialized at the class level
Instance variables
Declared with @
Available in the instance of the parent class
Why are parameters used in a method
Parameters are used when you have data outside of a method definition’s scope, but you need access to it within the method definition. If the method definition does not need access to any outside data, you do not need to define any parameters.
Default parameters
def say(words=’hello’)
Optional parentheses
For example, say() could be rewritten as just say. With arguments, instead of say(“hi”), it could just be say “hi”.
Method definition and local variable scope
A method definition creates its own scope outside the regular flow of execution. This is why local variables within a method definition cannot be referenced from outside of the method definition. It’s also the reason why local variables within a method definition cannot access data outside of the method definition (unless the data is passed in as a parameter).
Mutating caller methods
Methods that perform actions on the argument passed to a method which mutates the caller
a = [1, 2, 3]
Example of a method definition that modifies its argument permanently
def mutate(array)
array.pop
end
p “Before mutate method: #{a}”
mutate(a)
p “After mutate method: #{a}”
Pop mutates the caller
What is a conditional
a fork in the road.
our data approaches a conditional and the conditional then tells the data where to go based on some defined parameters.
How are conditionals formed?
Conditionals are formed using a combination of if statements and comparison operators (, <=, >=, ==, !=, &&, ||).
What’s the order of precedence?
Used when deciding how to evaluate multiple expressions. The following is a list of operations from highest order of precedence (top) to lowest (bottom).
<=, , >= - Comparison
==, != - Equality
&& - Logical AND
|| - Logical OR
Describe a case statement
Has similar functionality to an if statement but with a slightly different interface.
Case statements use the reserved words case, when, else, and end.
How do you loop?
loop do //something end
How do you skip the rest of the code in a loop and start with the next iteration?
By using next
How do you exit the loop immediately?
By using break
What is a while loop
A while loop is given a parameter that evaluates to a boolean (remember, that’s just true or false). Once that boolean expression becomes false, the while loop is not executed again, and the program continues after the while loop.
How to write a while loop
while boolean statement
do something
end
How to write an until loop
until boolean statement
do something
end
What’s an until loop
Opposite of while loop
What are the ways to use a do/while loop
loop do do something if this condition is true break out of loop end
begin
do something
end while condition
Difference between do while and while loops
Code in do while gets executed at least once
What are for loops used for?
for loops are used to loop over a collection of elements
What’s the difference between for loops and while loops
Unlike a while loop where if we’re not careful we can cause an infinite loop, for loops have a definite end since it’s looping over a finite number of elements
How do you write a for loop?
It begins with the for reserved word, followed by a variable, then the in reserved word, and then a collection of elements.
for i in 1..10 do
puts i
end
What is a range?
A range is a special type in Ruby that captures a range of elements. For example 1..3 is a range that captures the integers 1, 2, and 3.
What does a for loop return?
The collection of elements after it executes
What does a while loop return
nil
What method is used to decide if a number is not event?
odd?
What are iterators?
Iterators are methods that naturally loop over a given set of data and allow you to operate on each element in the collection.
What does each do?
loops through each elements of a collection and allows you to operate over each element
Explain how .each works
The block’s starting and ending points are defined by the curly braces {}. Each time we iterate over the array, we need to assign the value of the element to a variable. In this example we have named the variable name and placed it in between two pipes |. After that, we write the logic that we want to use to operate on the variable, which represents the current array element. In this case it is simply printing to the screen using puts.
What’s a block?
Lines of code ready to be executed, inside { } or a do..end
When do you use do…end
When performing multi-line operations
What is recursion
Another way to create a loop.
Recursion is the act of calling a method from within itself..
What does each_with_index do?
Iterates through a collection and makes available the index and the item for use
What does .pop do to an array?
removes last element in an array, and mutates the caller
What does “mutate the caller” mean?
permanently changes the caller
What is the caller?
the collection calling an instance method
What does push do?
adds an item to the end of an array
Shovel operator
«_space;- adds item to end of array
mutates the caller
Map method
The map method iterates over an array applying a block to each element of the array and returns a new array with those results.
What’s the other name you can use for map?
collect
Do map or collect mutate the caller?
These methods are not destructive (i.e., they don’t mutate the caller).
How do you know which methods mutate the caller or not?
You have to use the methods and pay attention to the output in irb; that is, you have to memorize or know through using it.
Why would you use delete_at
Delete a value at a certain index
How would you delete a value at a certain index?
delete_at
What does delete do
Deletes all instances of a provided value in an array
How do you delete all instances of a value in an array
Use .delete(value)
What does uniq do?
Deletes all duplicates in an array , returning the results as a new array. It does not modify the initial array
How can you make the uniq method forcibly mutate the caller?
Use !
Are uniq and uniq! the same methods?
No, they are different methods, one returns a new array while the other mutates the caller
How do you filter an array in Ruby
use the select {bloc}
What does .select do?
This method iterates over an array and returns a new array that includes any items that return true to the expression provided.
Does .select mutate the caller?
It does not, it returns an array with the filtered elements
! exception
Not all methods are destructive/ mutates the caller. Check the documentation.
Example of methods which are destructive but don’t have !
push / pop
What does unshift do?
Adds the value provided to the front of the array.
It is the opposite of .pop
What does to_s do to an array?
creates the string representation of the array