Skip to content

Instantly share code, notes, and snippets.

@r7kamura
Last active April 2, 2018 14:32
Show Gist options
  • Save r7kamura/4ea0e21125906ae242e1164ea49a61a1 to your computer and use it in GitHub Desktop.
Save r7kamura/4ea0e21125906ae242e1164ea49a61a1 to your computer and use it in GitHub Desktop.
module Fizz
def call(target, max, state)
if target % 3 == 0
state << "fizz"
end
super
end
end
module Buzz
def call(target, max, state)
if target % 5 == 0
state << "buzz"
end
super
end
end
module Printable
def call(target, max, state)
if state.empty?
puts target
else
puts state.join
end
super(target, max)
end
end
module Clearable
def call(target, max)
super(target, max, [])
end
end
class FizzBuzz
prepend Printable
prepend Buzz
prepend Fizz
prepend Clearable
alias_method :step, :call
def call(target, max)
if target != max
step(target + 1, max)
end
end
end
FizzBuzz.new.call(1, 100)
@r7kamura
Copy link
Author

r7kamura commented Apr 2, 2018

Inheritance tree

Object
|
`-FizzBuzz
  |
  `-Printable
    |
    `-Buzz
      |
      `-Fizz
        |
        `--Clearable

State map

START
|
v
Clearable#call <-.
|                |
(super)          |
|                |
v                |
Fizz#call        |
|                |
(super)          |
|                |
v                |
Buzz#call        |
|                |
(super)          |
|                |
v                |
Printable#call   |
|                |
(super)          |
|        (aliased from #step)
v                |
FizzBuzz#call ---'
|
v
END

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