Permalink
Browse files

Separate out code for array value counting to module

  • Loading branch information...
camillebaldock committed Sep 3, 2014
1 parent 87d156a commit 1376b91fba29151a1d0a96f2995939b7c8dfa578
Showing with 32 additions and 7 deletions.
  1. +10 −0 lib/array_helper.rb
  2. +1 −7 lib/hand.rb
  3. +21 −0 spec/array_helper_spec.rb
View
@@ -0,0 +1,10 @@
+module ArrayHelper
+ def value_occurence_count(array)
+ grouped_values = array.group_by { |i| i }
+ result = {}
+ grouped_values.each do |key, value|
+ result[key] = value.count
+ end
+ result
+ end
+end
View
@@ -1,4 +1,5 @@
class Hand
+ include ArrayHelper
attr_reader :cards
@@ -46,13 +47,6 @@ def suit_count
value_occurence_count(cards.map(&:suit))
end
- def value_occurence_count(array)
- grouped_values = array.group_by { |i| i }
- grouped_values.each do |key, value|
- grouped_values[key] = value.count
- end
- end
-
private
def has_four
View
@@ -0,0 +1,21 @@
+require "spec_helper"
+require "array_helper"
+
+describe ArrayHelper do
+
+ class ExampleClass
+ end
+
+ before(:each) do
+ @example_class = ExampleClass.new
+ @example_class.extend(ArrayHelper)
+ end
+
+ let(:sample_array) { [:a, 1, "b", "B", 1] }
+
+ it "gives the number of occurences of each value in the array" do
+ result = @example_class.value_occurence_count(sample_array)
+
+ expect(result).to eq({ :a => 1, 1 => 2, "b" => 1, "B" => 1 })
+ end
+end

0 comments on commit 1376b91

Please sign in to comment.