riffle 0.0.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0a462eefd0ef7b1494eec7847a0518a34dd3876
4
- data.tar.gz: 47e2520de2f35d39da9eabf321e333c2281ad5e4
3
+ metadata.gz: 0f072f9e01cd3dae017947eca483b6edb3ddbebd
4
+ data.tar.gz: 6cc6682bc9332e96f873745676f98b8489f0071f
5
5
  SHA512:
6
- metadata.gz: 84210dd35055df948f24c8c34c8754458da764227f0cee4c58b0ade7a8b91679dd009f4209596bff8dc1f78695f514381102502b583101d3129a933868cea7ec
7
- data.tar.gz: ad6d5c63e120fb904e81489895d4cb0ed9d6b32729166c4aeb2f12e56d06a43443f5ad5ab7ac05c1a180cef76d7e77ad4c50b9b1f7cf9cbdb2420930f300dd74
6
+ metadata.gz: b29b6efd71e529a0183447b10fbfc261f7aab102264491b5dd4738ed555c9cfbef3f9e6f7732c6791e4815ca463ad7bb807abb4403c5279700184595424fb6d4
7
+ data.tar.gz: 104592ccc714b3571bc754d86954d109efbd46af9275b02b7382c26e9eb9394c112dc9aa2e7075ff2190e5ab414ccadb7dc9910f33a1833d475130bc0af490a1
data/lib/riffle.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "riffle/version"
2
+ require "riffle/array"
2
3
  require "riffle/core_ext/array/riffle"
@@ -0,0 +1,58 @@
1
+
2
+ module Riffle
3
+ module Array
4
+ def riffle(*args)
5
+ result = []
6
+
7
+ # handle empty or insufficient args
8
+ if self == ::Array && args.length < 2
9
+ return args.empty? ? [] : args.first
10
+ elsif self.is_a?(::Array) && args.empty?
11
+ return self
12
+ end
13
+
14
+ opts = get_opts_from_args(args)
15
+
16
+ opts[:range] ||= (1..3)
17
+
18
+ unless args.select { |a| !a.is_a? ::Array }.empty?
19
+ raise ArgumentError, "All arguments must be of type `Array`"
20
+ end
21
+
22
+ # insert self at front of args list if self is an Array instance
23
+ args.unshift self if self.is_a?(::Array)
24
+
25
+ # Random.new(nil) explodes for some reason...
26
+ if opts[:seed].nil?
27
+ prng = Random.new
28
+ else
29
+ prng = Random.new(opts[:seed])
30
+ end
31
+
32
+ until args.flatten.empty?
33
+ args.each_with_index do |arg, i|
34
+ group_size = prng.rand(opts[:range])
35
+ if arg.length < group_size
36
+ result.concat arg
37
+ args[i] = []
38
+ else
39
+ result.concat arg.take(group_size)
40
+ args[i] = arg.drop group_size
41
+ end
42
+ end
43
+ end
44
+
45
+ result
46
+ end
47
+
48
+ private
49
+
50
+ def get_opts_from_args(args)
51
+ opts_passed?(args) ? args.pop : {}
52
+ end
53
+
54
+ def opts_passed?(args)
55
+ args.length > 1 && args.last.is_a?(Hash)
56
+ end
57
+ end
58
+ end
@@ -1,35 +1,5 @@
1
1
 
2
2
  class Array
3
- def riffle(*args)
4
- result = []
5
-
6
- return self if args.empty?
7
-
8
- opts = args.pop if args.length > 1 && args.last.is_a?(Hash)
9
-
10
- min_group_size = (opts && opts[:min_group_size]) || 1
11
- max_group_size = (opts && opts[:max_group_size]) || 3
12
-
13
- # insert self at front of args list
14
- args.unshift self
15
-
16
- unless args.select { |a| !a.is_a? Array }.empty?
17
- raise ArgumentError, "All arguments must be of type `Array`"
18
- end
19
-
20
- until args.flatten.empty?
21
- args.each_with_index do |arg, i|
22
- group_size = rand(min_group_size..max_group_size)
23
- if arg.length < group_size
24
- result.concat arg
25
- args[i] = []
26
- else
27
- result.concat arg.take(group_size)
28
- args[i] = arg.drop group_size
29
- end
30
- end
31
- end
32
-
33
- result
34
- end
3
+ include Riffle::Array
4
+ extend Riffle::Array
35
5
  end
@@ -1,3 +1,3 @@
1
1
  module Riffle
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.1"
3
3
  end
data/spec/array_spec.rb CHANGED
@@ -1,81 +1,29 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Array do
4
- it "Array#riffle should be defined" do
5
- [].respond_to?(:riffle).should be_true
6
- end
7
-
8
- it "returns self if no args are passed" do
9
- a = [1, 2, 3]
10
- a.riffle.should == a
11
- end
12
-
13
- it "requires all arguments to be of type `Array`, except when the last arg is an opts Hash" do
14
- expect { [].riffle 1 }.to raise_error(ArgumentError)
15
- expect { [].riffle [1], :foo }.to raise_error(ArgumentError)
16
- expect { [].riffle({}, [1]) }.to raise_error(ArgumentError)
17
-
18
- # allow an opts Hash to be passed as the last arg
19
- expect { [].riffle [1], { min_group_size: 1 } }.to_not raise_error
20
- end
21
-
22
- it "does not raise exceptions when empty args are passed" do
23
- expect { [].riffle [], [] }.to_not raise_error
24
- [].riffle([], []).should be_empty
25
- end
26
-
27
- it "supports more than two arguments" do
28
- numbers = [1, 2, 3, 4, 5, 6]
29
- letters = %w(a b c d e f)
30
- symbols = [:foo, :bar, :baz, :qux]
31
- result = numbers.riffle letters, symbols
32
-
33
- result.length.should == numbers.length +
34
- letters.length +
35
- symbols.length
36
-
37
- # filtering out integers from the result should be exactly equal to the
38
- # original `numbers` array. particularly the order should be the same
39
- result.select { |r| r.is_a? Integer }.should.eql? numbers
40
-
41
- # filtering out strings from the result should be exactly equal to the
42
- # original `letters` array. particularly the order should be the same
43
- result.select { |r| r.is_a? String }.should.eql? letters
44
-
45
- # filtering out strings from the result should be exactly equal to the
46
- # original `letters` array. particularly the order should be the same
47
- result.select { |r| r.is_a? Symbol }.should.eql? symbols
48
- end
49
-
50
- it "merges all argument arrays while maintaining the order of the original arrays in relation to themselves" do
51
- numbers = [1, 2, 3, 4, 5, 6]
52
- letters = %w(a b c d e f)
53
- result = numbers.riffle letters
54
-
55
- result.length.should == numbers.length + letters.length
56
-
57
- # filtering out integers from the result should be exactly equal to the
58
- # original `numbers` array. particularly the order should be the same
59
- result.select { |r| r.is_a? Integer }.should.eql? numbers
60
-
61
- # filtering out strings from the result should be exactly equal to the
62
- # original `letters` array. particularly the order should be the same
63
- result.select { |r| r.is_a? String }.should.eql? letters
64
- end
65
-
66
- it "maintains the first element in the array on which `riffle` was called" do
67
- a = [1, 2, 3]
68
- b = %w(a b c)
69
- a.riffle(b).first.should == a.first
70
- end
71
-
72
- it "supports defining a min and max group size through an opts Hash passed as the last arg" do
73
- a = [1, 2, 3, 4, 5, 6, 7, 8]
74
- b = %w(a b c d e f g h)
75
- r = a.riffle(b, { min_group_size: 2, max_group_size: 2 })
76
- r.slice(0..1).each { |x| x.is_a?(Integer).should be_true }
77
- r.slice(2..3).each { |x| x.is_a?(String).should be_true }
78
- r.slice(6..7).each { |x| x.is_a?(String).should be_true }
79
- r.slice(8..9).each { |x| x.is_a?(Integer).should be_true }
4
+ context "#riffle" do
5
+ it "defines Array#riffle" do
6
+ [].respond_to?(:riffle).should be_true
7
+ end
8
+
9
+ it "returns self if no args are passed" do
10
+ a = [1, 2, 3]
11
+ a.riffle.should == a
12
+ end
13
+ end
14
+
15
+ context ".riffle" do
16
+ it "defines Array.riffle" do
17
+ Array.respond_to?(:riffle).should be_true
18
+ end
19
+
20
+ it "returns [] if no args are passed" do
21
+ Array.riffle.should == []
22
+ end
23
+
24
+ it "returns the arg if only one arg is passed" do
25
+ a = [1,2,3]
26
+ Array.riffle(a).should == a
27
+ end
80
28
  end
81
29
  end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ describe Riffle do
4
+ it "requires all arguments to be of type `Array`, except when the last arg is an opts Hash" do
5
+ expect { [].riffle 1 }.to raise_error(ArgumentError)
6
+ expect { [].riffle [1], :foo }.to raise_error(ArgumentError)
7
+ expect { [].riffle({}, [1]) }.to raise_error(ArgumentError)
8
+
9
+ # allow an opts Hash to be passed as the last arg
10
+ expect { [].riffle [1], { min_group_size: 1 } }.to_not raise_error
11
+ end
12
+
13
+ it "does not raise exceptions when empty args are passed" do
14
+ expect { [].riffle [], [] }.to_not raise_error
15
+ [].riffle([], []).should be_empty
16
+ end
17
+
18
+ it "supports more than two arguments" do
19
+ numbers = [1, 2, 3, 4, 5, 6]
20
+ letters = %w(a b c d e f)
21
+ symbols = [:foo, :bar, :baz, :qux]
22
+ result = numbers.riffle letters, symbols
23
+
24
+ result.length.should == numbers.length +
25
+ letters.length +
26
+ symbols.length
27
+
28
+ # filtering out integers from the result should be exactly equal to the
29
+ # original `numbers` array. particularly the order should be the same
30
+ result.select { |r| r.is_a? Integer }.should.eql? numbers
31
+
32
+ # filtering out strings from the result should be exactly equal to the
33
+ # original `letters` array. particularly the order should be the same
34
+ result.select { |r| r.is_a? String }.should.eql? letters
35
+
36
+ # filtering out strings from the result should be exactly equal to the
37
+ # original `letters` array. particularly the order should be the same
38
+ result.select { |r| r.is_a? Symbol }.should.eql? symbols
39
+ end
40
+
41
+ it "merges all argument arrays while maintaining the order of the original arrays in relation to themselves" do
42
+ numbers = [1, 2, 3, 4, 5, 6]
43
+ letters = %w(a b c d e f)
44
+ result = numbers.riffle letters
45
+
46
+ result.length.should == numbers.length + letters.length
47
+
48
+ # filtering out integers from the result should be exactly equal to the
49
+ # original `numbers` array. particularly the order should be the same
50
+ result.select { |r| r.is_a? Integer }.should.eql? numbers
51
+
52
+ # filtering out strings from the result should be exactly equal to the
53
+ # original `letters` array. particularly the order should be the same
54
+ result.select { |r| r.is_a? String }.should.eql? letters
55
+ end
56
+
57
+ it "maintains the first element in the array on which `riffle` was called" do
58
+ a = [1, 2, 3]
59
+ b = %w(a b c)
60
+ a.riffle(b).first.should == a.first
61
+ end
62
+
63
+ it "supports defining a min and max group size through an opts Hash passed as the last arg" do
64
+ a = [1, 2, 3, 4, 5, 6, 7, 8]
65
+ b = %w(a b c d e f g h)
66
+ r = a.riffle(b, { range: (2..2) })
67
+ r.slice(0..1).each { |x| x.is_a?(Integer).should be_true }
68
+ r.slice(2..3).each { |x| x.is_a?(String).should be_true }
69
+ r.slice(6..7).each { |x| x.is_a?(String).should be_true }
70
+ r.slice(8..9).each { |x| x.is_a?(Integer).should be_true }
71
+ end
72
+
73
+ it "supports passing a custom seed" do
74
+ a = [1, 2, 3, 4, 5, 6, 7, 8]
75
+ b = %w(a b c d e f g h)
76
+ r1 = Array.riffle(a, b, seed: 123)
77
+ r2 = Array.riffle(a, b, seed: 123)
78
+ expect(r1).to eql(r2)
79
+ end
80
+ end
81
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riffle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Stephens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-27 00:00:00.000000000 Z
11
+ date: 2013-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,10 +66,12 @@ files:
66
66
  - README.md
67
67
  - Rakefile
68
68
  - lib/riffle.rb
69
+ - lib/riffle/array.rb
69
70
  - lib/riffle/core_ext/array/riffle.rb
70
71
  - lib/riffle/version.rb
71
72
  - riffle.gemspec
72
73
  - spec/array_spec.rb
74
+ - spec/riffle_spec.rb
73
75
  - spec/spec_helper.rb
74
76
  homepage: http://github.com/jordanstephens/riffle
75
77
  licenses:
@@ -91,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
93
  version: '0'
92
94
  requirements: []
93
95
  rubyforge_project:
94
- rubygems_version: 2.0.0
96
+ rubygems_version: 2.0.3
95
97
  signing_key:
96
98
  specification_version: 4
97
99
  summary: riffle multiple Arrays by selecting a random number of items from each and
@@ -99,5 +101,5 @@ summary: riffle multiple Arrays by selecting a random number of items from each
99
101
  have been merged.
100
102
  test_files:
101
103
  - spec/array_spec.rb
104
+ - spec/riffle_spec.rb
102
105
  - spec/spec_helper.rb
103
- has_rdoc: