Skip to content

Instantly share code, notes, and snippets.

@erich
Created December 12, 2013 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erich/7932498 to your computer and use it in GitHub Desktop.
Save erich/7932498 to your computer and use it in GitHub Desktop.
#This could work:
class MegaLotto
NUMBERS = 5
def self.draw
NUMBERS.times.inject([]) do |arr, i|
arr << self.random_draw
end
end
private
def self.random_draw
rand(0...60)
end
end
LotteryDrawing.draw
#However I don't like the inject method, because it is needles here, constant and private ... IMHO it would look much better:
require 'minitest/autorun'
class MegaLotto
def draw(number_of_results: 5, range: 0...60)
number_of_results.times.collect { random_draw(range) }
end
def random_draw(range)
rand(range)
end
end
class TestMegaLotto < MiniTest::Test
def setup
@mega_loto = MegaLotto.new
@mega_loto_draw = @mega_loto.draw
end
def test_implements_draw_and_random_draw
assert_respond_to @mega_loto, :random_draw
assert_respond_to @mega_loto, :draw
end
def test_draw_is_array
assert_kind_of Array, @mega_loto_draw
end
def test_draw_is_random
refute_equal MegaLotto.new.draw, @mega_loto_draw
end
end
#Or maybe:
require 'minitest/autorun'
class MegaLotto
def draw(number_of_results: 5, range: 0...60)
range.to_a.sample(number_of_results)
end
end
class TestMegaLotto < MiniTest::Test
def setup
@mega_loto = MegaLotto.new
@mega_loto_draw = @mega_loto.draw
end
def test_implements_draw
assert_respond_to @mega_loto, :draw
end
def test_draw_is_array
assert_kind_of Array, @mega_loto_draw
end
def test_draw_is_random
refute_equal MegaLotto.new.draw, @mega_loto_draw
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment