i'm working array looks this:
cards = [#<cardshark::card:0x9200cc @rank=:ace, @suit=:coins>, etc]
each of 4 suits (coins, swords, cups, clubs) contains ranks ace through 7 , 3 face cards total of 40 cards per "deck"
i looking write rspec test verify array contains 4 suits. started going down path of using @cards.select
block using regular expressions , got ugly pretty fast.
what's best way handle this?
try using enumerable#group_by:
num_suits = cards.group_by { |card| card.suit }.length
in irb:
~$ irb >> groups = (1..10).group_by { |n| n % 4 } => {0=>[4, 8], 1=>[1, 5, 9], 2=>[2, 6, 10], 3=>[3, 7]} >> groups.length => 4
Comments
Post a Comment