GithubHelp home page GithubHelp logo

oo-composition-demo's Introduction

Object Composition Live-Code

Learning Goals

  • Have concrete examples and strategies of Ruby syntax that model one-to-one relationships
  • Have concrete examples and strategies of Ruby syntax that model one-to-many relationships

What's in this Repo

  • main.rb
    • This is a file that loads all of the other files. It contains recommended comments and puts statements about what to cover and how, though instructors should feel empowered to use or change whatever feels most appropriate
  • order.rb
    • Defines the class Order with the attributes id and shipment
  • shipment.rb
    • Defines the class Shipment with the attributes tracking_number
  • merchant.rb
    • Defines the class Merchant with the attributes name and products
  • product.rb
    • Defines the class Product with the attribute name

Instructors

You should be modifying your code on a new branch that is your class name, ie leaves, branches, sockets, ports, etc.

One to One Relationships

one-to-one relationship of order and shipment

One to one relationships should be tracked using instance variables, named appropriately and singular, and assigning values to them.

To summarize:

  • To read one-to-one relationships, we read instance variables using the dot syntax
  • To set one-to-one relationships, we should be assigning values to instance variables

Demo

Run through the first comment section in main.rb.

Questions for during or after the Demo

Note the attr_accessor in the Order class. What attribute is attached to attr_accessor? Why couldn't it be attr_reader?

Optional: What alternative syntax could we use?

One to Many Relationships

one-to-many relationship of merchant and product

One to many relationships should be tracked using instance variables, named appropriately and plural, and creating an empty array and modifying the array.

To summarize:

  • To read one-to-many relationships, we read instance variables using the dot syntax and get back an array
  • To set one-to-one relationships, we should be assigning values to instance variables, or pushing/modifying existing instance variables

Demo

Questions for during or after the Demo

Note the attr_reader in the Merchant class. What attributes are attached to attr_reader? Why do we not need attr_accessor?

oo-composition-demo's People

Contributors

dhelmgren avatar tildeee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.