Ruby basics Flashcards
(187 cards)
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