Quiz: Lesson 2
-
@a = 2
user = User.new
user.name
user.name = "Joe"
value equal to 'Joe'
-
How does a class mixin a module?
-
What's the difference between class variables and instance variables?
instances of the class. Class variables are prefixed with '@@' where as instance variables are prefixed with '@'. An instance variable is unique to each object.
-
What does attr_accessor do? It defines getter and setter methods automatically, reducing the amount of code that is required.
-
Dog.some_method is a class method
-
In Ruby, what's the difference between subclassing and mixing in modules? Subclassing is used in hierarchical structures and were relationships can be described as 'is-a'. Modules are used in 'has-a' relationships.
-
Given that I can instantiate a user like this: User.new('Bob'), what would the initialize method look like for the User class? Class User attr_accessor :name
def initialize(name) @name = name end end
-
Can you call instance methods of the same class from other instance methods? Yes, you have to prefix 'self' when calling the method.
-
When you get stuck, what's the process you use to try to trap the error? Using pry: "require 'pry'"" and then use "binding.pry" in the suspected area, eliminating sections as you move through the code.