Skip to content

Instantly share code, notes, and snippets.

@W-Mills
Last active December 17, 2018 15:59
Show Gist options
  • Save W-Mills/568fb726ae79609e2a664c79f2e49eae to your computer and use it in GitHub Desktop.
Save W-Mills/568fb726ae79609e2a664c79f2e49eae to your computer and use it in GitHub Desktop.
# recreation_centre_last_example.rb
module Swimmable
def swim
"Can swim here!"
end
end
class GreenSpace
attr_accessor :name, :num_trees
def initialize(name, num_trees)
@name = name
@num_trees = num_trees
end
end
class CityPark < GreenSpace
attr_accessor :rec_centre
end
class RecreationCentre
attr_reader :philanthropist
attr_accessor :name
include Swimmable
def initialize(name, philanthropist)
@name = name
@philanthropist = philanthropist
end
def to_s
name
end
def whats_this
self
end
end
class Forest < GreenSpace; end
class Lake < Forest
include Swimmable
end
alexandria_park = CityPark.new("Alexandria Park", 2500)
wallace_emerson = RecreationCentre.new("Wallace Emerson", "Joe Beef")
scadding_court = RecreationCentre.new("DunBat", "Jim Balsillie")
scadding_court.name # => "DunBat"
scadding_court.name = "Scadding Court"
scadding_court.name # => "Scadding Court"
scadding_court.whats_this # => #<RecreationCentre:0x00007fc73b9a0060 @name="Scadding Court", @philanthropist="Jim Balsillie">
alexandria_park.rec_centre = scadding_court
puts alexandria_park.rec_centre # Scadding Court (because of our polymorphic new to_s method)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment