Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md
ruby-object_notes.pdf

README.md

LESSON: ruby-object


2018-07-05

OBJECTIVES

By the end of this, developers should be able to:

  • Define a class for an object in Ruby that assigns attributes in the initialize constructor.
  • Create an instance of an object in Ruby using .new.
  • Write setter and getter instance methods for Ruby objects.

VOCABULARY

Abstract Data Type: A description of a data type that is abstracted away from how it is implemented in any language in particular ie a logical representation of a data type that can be implemented in a language-independent way. For example, a list is an Abstract Data Type which is defined as a collection of values that are ordered by Index. In Javascript and Ruby, lists are implemented as Array types with particular properties and methods, albiet Javascript and Ruby Arrays have different properties and methods, but share basic structure as defined in the Abstract Data Type. In Python, a list is implemented as a List type, again with somewhat unique properties and methods, but sharing the same basice structure as well.

Object: An object is an Abstract Data Type that consists of a collection of properties and methods that are indexed by key, ie key-value pairs. Objects can be used to model real world objects.

Object Constructor: A function / method that runs when you instantiate a new Object either from a class (Ruby), or from a Constructor Function (Javascript), giving the new instance particular initial properties and methods.

Class: A blueprint for an object, defining the properties it receives on instantiation, as well as instance methods. Some language such as Ruby fundamentally use classes to define objects. In fact in Ruby, objects can only be defined using classes and instantiation from classes. In Javascript, objects are fundamentally built using Constructors and Protoytypes, but recently, the class keyword as syntactic sugar was added to Javascript to allow people from languages that use classes to feel more comfortable with Javascript, however under the hood, Javascript fundamentally uses Constructors and Prototypes.

Setter: An instance method that allows you to set values of properties in a class. In Javascript, you can directly set existing properties on an object or make new one. In Ruby, object properties are private, so you need to define a setter method if you want to set them.

Getter: An instance method that allows you to get values of properties in a class. In Javascript, you can directly get existing properties on an object. In Ruby, object properties are private, so you need to define a getter method if you want to get them.

Mutability: A variable is mutable if it's value / shape can be changes after it's created.

NOTES

  • Ruby does not allow for object literal notation. Objects are only created by first creating classes, then using <ClassName>.new
  • Object-oriented langauges such as Ruby strictly use classes to generate objects, and usually have objects representing nearly every type of value. Other language may not be strictly Object-oriented, but can support object-oriented programming styles, such as Javascript, which uses Constructor Functions and Prototypes under the hood, but provide the class keyword to create objects as well
  • There are two ways to interact with Ruby
    • running script files using ruby <script file name>
      • you can use binding.pry inserted into scripts to define a debugging breakpoint in your code, where you can access all of the scope of the script up until that point and run Ruby commands interactively.
        • binding.pry cannot be the last line in a script. Add "" to the end of the script after binding.pry if you want binding.pry at the end.
        • you can exit out of a binding.pry breakpoint and keep running the code in a script using the continue keyword
    • running a Ruby REPL:
      • irb the built-in REPL for Ruby
      • pry the more modern REPL for Ruby with some updated features

EXAMPLES

Javascript Supports Classes (but they are syntactic sugar)

  • in Javascript, we normally make Objects using Constructors:

  • function Dog(name, breed) {
        this.name = name
        this.breed = breed
    }
  • and Prototypes to add instance methods:

  • Dog.prototype.bark = function () {
        console.log('bork.')
    }
  • you can also use the class keyword to achieve the same thing:

    // this is how you make a class definition
    class Dog {
    
        // notice how the contents of the Constructor 
        // function go in this class method called          
        // `constructor`
        constructor() {
            this.name = name
            this.breed = breed
        }
        
        // instead of adding instance methods using 
        // `prototype`, you can add them directly
        // in the class defintion as class methods
        bark () {
            console.log('bork.')
        }
    }
  • both work exactly the same as far as the end result. You can instantiate a Dog object in both cases using:

  • const fido = new Dog('Fido', 'Dobberman')

Javascript Classes vs Ruby Classes

  • Javascript class:

  • class Rectangle {
      constructor(length, width) {
        this.length = length
        this.width = width
      }
    
      area () {
        return this.length * this.width
      }
    }
  • Ruby class:

  • class Rectangle
      def initialize(length, width)
        @length = length
        @width = width
      end
    
      def area
        @length * @width
      end
    end
  • Notice how similar the Javascript class syntax is to the Ruby class syntax. In fact, they are almost directly translatable from one to the other, simply by replacing keywords and block demarcations.