0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FizBuz OOP way

0
Last updated at Posted at 2015-07-17

Sandi Metzのレクチャーを参照しながらFizBuz

# 3 - fiz, 5 - buz, 15 - fizbuz
# 2 - Hadoken, 7 - sonicboom, 14 - kaioh-ken.....
# you can add&delete whatever any kind of things message, methods, class....
 
require 'pry'
 
class DoFizBuz
 
  attr_reader :int
  attr_accessor :divisible_by_which
 
  DEVIDER = [3, 5, 15, 2, 7, 14, 100, 28].sort.reverse
 
  def initialize(int)
    @int = int
    @divisible_num = divisible_num
  end
  
  def divisible_num
    DEVIDER.find{|num| int%num == 0}
  end
 
  def factory
    if divisible_num == nil 
      p int
    else
      Object.const_get("Message#{divisible_num}").new.message
    end
  end
 
end
 
class Message3
  def message
   p "Fiz"
  end
end
 
class Message5
  def message
    p "Buz"
  end
end
 
class Message15
  def message
   p "FizBuz"
  end
end
 
class Message2
  def message
   p "Ha doh ken"
  end
end
 
class Message7
  def message
   p "Show Ryu Ken"
  end
end
 
class Message14
  def message
   p "Shin-ku Ha doh ken!!"
  end
end
 
class Message28
  def message
  p "Time is - #{Time.now} - now "
  end
end
 
 
class Message100
  def message
   p "Kah- Mae- Ha- Mae- Ha---!!!!"
  end
end
 
 
(1..100).to_a.each{|num| DoFizBuz.new(num).factory}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?