Skip to content

Instantly share code, notes, and snippets.

@jamesyang124
Last active December 26, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesyang124/7156897 to your computer and use it in GitHub Desktop.
Save jamesyang124/7156897 to your computer and use it in GitHub Desktop.

Ruby Week 2 Quiz

  1. 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
  1. How does a class mixin a module?
  • By inculde a module, and module provides the instance method for that instance object.
  1. 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.
  1. What does attr_accessor do?
  • an getter&setter method annotation.
  1. How would you describe this expression: Dog.some_method
  • It is a class method call by a class Dog object.
  1. 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.
  1. 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
  1. 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.
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment