mirror of
https://github.com/tomgi/git_stats.git
synced 2025-01-03 11:12:11 +01:00
hash#to_key_indexed_array spec
This commit is contained in:
parent
c5c80cb382
commit
3a29083414
2 changed files with 17 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
||||||
class Hash
|
class Hash
|
||||||
def to_key_indexed_array
|
def to_key_indexed_array
|
||||||
self.inject([]) { |acc, (k, v)| acc[k] = v; acc }
|
raise ArgumentError.new('all the keys must be numbers to convert to key indexed array') unless all? { |k, v| k.is_a? Numeric }
|
||||||
|
inject([]) { |acc, (k, v)| acc[k] = v; acc }
|
||||||
end
|
end
|
||||||
end
|
end
|
15
spec/hash_extension_spec.rb
Normal file
15
spec/hash_extension_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Hash do
|
||||||
|
context 'to_key_indexed_array' do
|
||||||
|
it 'should convert hash to array using keys as indexes' do
|
||||||
|
hash = {1 => 'x', 2 => 1, 5 => 'a'}
|
||||||
|
hash.to_key_indexed_array.should == [nil, 'x', 1, nil, nil, 'a']
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should throw exception if not all of the keys are numbers' do
|
||||||
|
hash = {1 => 'x', 'b' => 2}
|
||||||
|
expect { hash.to_key_indexed_array }.to raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue