Coder Social home page Coder Social logo

ruby-collaborating-objects-readme's Introduction

Ruby Collaborating Objects Readme

Objective

  1. Gain a deeper understanding of object relations
  2. Use other classes and methods within another class to collaboratively send messages to one another

Introduction

Let's stick with our song/artist example. Our song class is responsible for handling songs. Our artist class is responsible for handling artists. However, these things clearly have some relation to one another. Remember, a song belongs to an artist, and an artist has many songs. These two classes will have to collaborate.

In fact, the classes do not even need to have any relationship ("has many" or "belongs to") to collaborate. Imagine we have an MP3 Importer that is responsible for taking in a bunch of MP3 files and making a song for each unique filename. It is not hard to imagine that to make a song, the MP3 Importer will have to have some sort of communication with the Song class.

Let's take a look at each of these collaborations in more detail.

MP3 Importer collaborating with Songs

The purpose of this MP3 Importer is to take in a list of mp3s and send each mp3 filename to the Song class to make a Song. Let's just focus on the collaboration. Our MP3Importer class will receive a list of filenames that look like this "Drake - Hotline Bling". MP3Importer will then send each of those filenames to the Song class to be created.

class Song
  attr_accessor :title

  def self.new_by_filename(filename)
    song = self.new
    song.title = filename.split(" - ")[1]
    song
  end

end

class MP3Importer
  def import(list_of_filenames)
    list_of_filenames.each{ |filename| Song.new_by_filename(filename) }
  end
end

Notice how within the MP3Importer class we are calling the Song class and a method within the Song class: .new_by_filename.

When we hit this line of code, it will send us to the Song class to do whatever behavior we have defined in the .new_by_filename class method. Then we will return to the MP3Importer class to continue executing the code. This is at the heart of collaborating objects.

Songs collaborating with Artists

Since our song belongs to an artist, we will want to collaborate with the Artist class at some point. Imagine we have the following code:

class Song
  attr_accessor :artist

  # other methods

  def artist_name=(name)
    if (self.artist.nil?)
      self.artist = Artist.new(name)
    else
      self.artist.name = name
    end
  end
end
class Artist
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  # other methods

end

The point of this code is that we want to be able to execute the following code given a song hotline_bling = Song.new('Hotline Bling') (Let's use Hotline Bling by Drake):

hotline_bling.artist_name = "Drake"
hotline_bling.artist

This should then return the new Artist object that was created by the #artist_name method.

View Ruby Collaborating Objects Readme on Learn.co and start learning to code for free.

ruby-collaborating-objects-readme's People

Contributors

annjohn avatar aviflombaum avatar ipc103 avatar jmburges avatar joshuabamboo avatar kaylee42 avatar octosteve avatar pletcher avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruby-collaborating-objects-readme's Issues

Contrary to text, MP3Importer class seems to have a 'has many' relationship w/ Song class

From README.md:

In fact, the classes do not even need to have any relationship ("has many" or "belongs to") to collaborate. Imagine we have an MP3 Importer that is responsible for taking in a bunch of MP3 files and making a song for each unique filename.
.
.
.

class MP3Importer
attr_accessor :songs 
  def import(list_of_filenames)
    list_of_filenames.each{|file_name| songs << Song.new_by_filename(file_name)}
  end
end

Maybe I'm missing something, but it seems like the MP3Importer class has a 'has many' relationship with the Song class. I understand that the 'real-world model' of an MP3 importer doesn't have that type of relationship with the MP3 files that it imports, but, by storing all of the imported Song objects in a @songs array within each MP3Importer object, it certainly seems to have that type of relationship here.

Perhaps use the MP3Importer class as only a factory for new Song objects from a passed-in list, without storing the values in @songs?

/pedantic

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.