- Name what each of the below is:
a = 1 # => ex. a is a local variable, and is a Fixnum object with value 1
@a = 2 # => @a is instance vairiable, Fixnum object.
user = User.new # => local instance, is an User object.
user.name # => instance getter method
user.name = "Joe" # => instance setter method, set something to String object
- How does a class mixin a module?
- By inculde a module, and module provides the instance method for that instance object.
- What's the difference between class variables and instance variables?
- class variables scope is defined by class level, instance variables bind its life cycle with particular object.
- What does
attr_accessor
do?
- an getter&setter method annotation.
- How would you describe this expression:
Dog.some_method
- It is a class method call by a class Dog object.
- In Ruby, what's the difference between subclassing and mixing in modules?
- Mixing provides multi-inheritance, Subclassing is singule inheritance. The design to pick one of each is by the principle: An sub-class is an fork of its parent or is borrow partial properties from the super class.
- Given that I can instantiate a user like this:
User.new('Bob')
, what would the initialize
method look like for the User
class?
def initialize(str); @str = str; end
- Can you call instance methods of the same class from other instance methods in that class?
- Yes, but have to add require, mixin, or sublclassing to achieve this.
- When you get stuck, what's the process you use to try to trap the error?
binding.pry
, irb, or ruby -r debug
for ruby 1.8.7