Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created November 24, 2014 07:52
Show Gist options
  • Save JoshCheek/d740052fe9e2c666d15c to your computer and use it in GitHub Desktop.
Save JoshCheek/d740052fe9e2c666d15c to your computer and use it in GitHub Desktop.
Fun times at Rubyconf!
class Treehouse
def initialize(ladder, happyduckslide, sadsharkslide)
@ladder = ladder # => #<StringIO:0x007fb29c0c8a68>
@happyduckslide = happyduckslide # => #<IO:<STDOUT>>
@sadsharkslide = sadsharkslide # => #<IO:<STDERR>>
end # => :initialize
def ride
name = @ladder.gets().chomp() # => "Josh", "Anna Marie"
if rand(2) == 1 # => true, false
@happyduckslide.puts("#{name} climbs the ladder, slides into the lake, and swims with the ducks!") # => nil
else
@sadsharkslide.puts("#{name} climbs the ladder, slides into the ocean, and gets devoured by the shark!") # => nil
end # => nil, nil
end # => :ride
end
require 'stringio' # => true
ladder = StringIO.new "Josh\nAnna Marie" # => #<StringIO:0x007fb29c0c8a68>
happyduckslide = $stdout # => #<IO:<STDOUT>>
sadsharkslide = $stderr # => #<IO:<STDERR>>
treehouse = Treehouse.new(ladder, happyduckslide, sadsharkslide) # => #<Treehouse:0x007fb29c0c8518 @ladder=#<StringIO:0x007fb29c0c8a68>, @happyduckslide=#<IO:<STDOUT>>, @sadsharkslide=#<IO:<STDERR>>>
treehouse.ride # => nil
treehouse.ride # => nil
# There's a super cool new treehouse down the block.
# It has a ladder that takes a person into the house and two slides that drops them out into a lake.
# Anyone can go for a slide ride.
#
# >> Josh climbs the ladder and slides into the lake!
# !> Anna Marie climbs the ladder and slides into the lake!
@codesliced
Copy link

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