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 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