Skip to content

Instantly share code, notes, and snippets.

@buwilliams
Created August 28, 2017 15:00
Show Gist options
  • Save buwilliams/e4f7588b5e7121bdb31bce584d245150 to your computer and use it in GitHub Desktop.
Save buwilliams/e4f7588b5e7121bdb31bce584d245150 to your computer and use it in GitHub Desktop.
class Dog
def speak
puts 'Ruff! Grr! Roof! Rawr!'
end
end
class Cat
def speak
puts 'Meeeow...'
end
end
class Bird
def speak
puts 'Tweety, tweet, tweet'
end
end
class AnimalShelter
attr_accessor :animals
def initialize
@animals = Array.new
end
def add(animal)
@animals << animal
end
def open_doors
animals.each do |animal|
animal.speak
end
end
end
animal_shelter = AnimalShelter.new
3.times { animal_shelter.add Cat.new }
2.times { animal_shelter.add Dog.new }
4.times { animal_shelter.add Bird.new }
animal_shelter.open_doors
# Explorations:
# 1. How might we sort/get data on animals without adding coupling to
# AnimalShelter? (interweave, specify order, 1 of each, count each)
# 2. How could we change the mechanism for output without changing all the
# animal classes?
# 3. How would the impl be different if we used Ruby's Enumerable for
# AnimalShelter?
@kyledcline
Copy link

kyledcline commented Aug 28, 2017

  1. How might we sort/get data on animals without adding coupling to AnimalShelter?

This is actually pretty cool. Note you can use inheritance or module inclusion to make your life easier, but neither are strictly necessary (I'll use inheritance with Animal as base class here). I am using something arbitrary like weight which gives us a business case for sorting. First let's create a base class and then ducktype the subclasses.

class Animal
  def weight
    fail 'Subclass must implement #weight'
  end
end

class Dog < Animal
  # ...
  def weight
    80 # lb
  end
end

class Cat < Animal
  # ...
  def weight
    20 # lb
  end
end

class Bird < Animal
  # ...
  def weight
    2 # lb
  end
end

Now each animal has a weight attribute associated with it, which is something we can actually perform sorting on. Any animal that doesn't have a weight defined will raise an exception. Now I'll extend the classes with Comparable (part of ruby stdlib).

class Animal
  include Comparable

  # ...

  protected

  def <=>(other)
    weight <=> other.weight
  end
end

By including Comparable and providing a definition for the spaceship operator, we can now directly compare animals to each other. For sake of simplicity, I'm essentially just delegating the comparison to the weight method on each animal. This is really powerful. Anything you can imagine with relation to sorting you now get for free.

Before if we run animal_shelter.animals.sort, we'll get an exception like ArgumentError: comparison of Cat with Dog failed as expected. Now we can easily sort.

@kyledcline
Copy link

  1. How would the impl be different if we used Ruby's Enumerable for AnimalShelter?

The implementation will get a lot cooler and feel a lot more intuitive. We just extend AnimalShelter with Enumerable and define the each method.

class AnimalShelter
  include Enumerable

  def each
    animals.each
  end

That's it. Now every enumerable operation (see http://devdocs.io/ruby~2.2/enumerable for all the methods) we get for free.

animal_shelter.sort
animal_shelter.select { |animal| animal.weight > 35 } # only dogs will get returned now
animal_shelter.all? { |animal| animal.is_a? Animal } # true if all elements of the collection are Animals
animal_shelter.count # 9
animal_shelter.group_by(&:class)
# {
#   Cat  => [#<Cat:0x007fee33cf01b0>, #<Cat:0x007fee33cf0188>, #<Cat:0x007fee33cf0160>], 
#   Dog  => [#<Dog:0x007fee33cc0348>, #<Dog:0x007fee33cc02f8>], 
#   Bird => [#<Bird:0x007fee33c988c0>, #<Bird:0x007fee33c98898>, #<Bird:0x007fee33c98870>, #<Bird:0x007fee33c98820>]
# }

@kyledcline
Copy link

kyledcline commented Aug 28, 2017

  1. How could we change the mechanism for output without changing all the animal classes?

Here's a way to generalize speech. Move speak from each animal and place it in Animal superclass, then define speech for each animal with just a string.

class Animal
  # ...
  def speak(&block)
    block ||= ->(str) { puts str }
    block.call(speech)
  end
end

class Dog < Animal
  # ...
  def speech
    'Ruff! Grr! Roof! Rawr!'
  end
end

# same with Cat and Bird

The interesting change here is the speak method implementation. First we've objectified the block (if any) in the argument list - the & keyword turns a block into an object (specifically a proc), or turns a proc into a block.

Then we've basically said "block is equal to block unless I didn't give you anything, in which case it's equal to a proc that contains a puts statement".

Dog.new.speak
  "Ruff! Grr! Roof! Rawr!"
  => nil
# runs #puts on speech

Dog.new.speak do |s|
  file = File.open('spoken.txt', ?w)
  file << s
  file.close
end
# opens a new file called spoken.txt and writes the speech to it

Basically you can give the speak method any writer you want in its block during runtime. You can definitely get fancier, but that's a basic start.

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