Skip to content

Instantly share code, notes, and snippets.

@g1na1011
Last active December 23, 2015 03:29
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 g1na1011/6573633 to your computer and use it in GitHub Desktop.
Save g1na1011/6573633 to your computer and use it in GitHub Desktop.
Below are the answers to our second week's quiz from Course 1 of Tealeaf Academy

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 an instance variable, and is a Fixnum object with value 2
  
  user = User.new 
  # => user is a new instance object of the class User
  
  user.name 
  # => the object user is calling the name getter instance method
 
 user.name = "Joe" 
 # => the object user is calling the name setter instance method, resetting the object's name string value to "Joe"
  1. How does a class mixin a module?

    module ModExample
      # code for module here
    end
    
    class Animal
      include ModExample
      
      # code for class here
    end
    
  2. What's the difference between class variables and instance variables?

    Answer: A class variable is shared among the class and all of its descendants. It also begins with two @@ symbols. An instance variable is not shared by the class' descendants and it begins with one @ symbol.

  3. What does attr_accessor do?

    Answer: attr_accessor is a shortcut to define a class' getter and setter methods.

    class Dog
      attr_accessor :name
    end
    
    # This is the same without using 'attr_accessor'. 
    class Dog
      def name
        @name
      end
      
      def name=(new_name)
        @name = new_name
      end
    end
    
  4. How would you describe this expression: Dog.some_method

    Answer: The class Dog is calling a class method called some_method.

  5. In Ruby, what's the difference between subclassing and mixing in modules?

    Answer:

    Subclassing is when the classes is a relationship and has a single inheritance. For example, a Dog subclass will inherit all of the behaviors of an Animal superclass.

    Mixing in modules is called composition and it is when classes has a relationship. This is usually when classes have multiple inheritances or when certain behaviors are shared among classes, but not every single one of them.

  6. 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)
        self.name = name
      end
    end
    
  7. Can you call instance methods of the same class from other instance methods in that class?

    Answer: Yes, you may call instance methods of the same class from other methods in that class.

  8. When you get stuck, what's the process you use to try to trap the error?

    Answer: Use the pry gem to debug your code. Insert binding.pry to each of the sections in your code that you wish to debug. Trap the error by eliminating the non-problematic areas.

    require "pry"
    
    # Ruby code
    # Ruby code
    

=> Put "binding.pry" wherever you would like debugging to start.

=> CTRL+D in your terminal will continue the execution. Remove "binding.pry" when done.

binding.pry

Ruby code

Ruby code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment