permutations 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -31,29 +31,23 @@ end
31
31
 
32
32
  desc 'Show files missing from gemspec'
33
33
  task :diff do
34
- files = %w[
35
- Rakefile
36
- *README* *readme*
37
- *LICENSE*
38
- *.gemspec deps.rip
39
- bin/*
40
- lib/**/*
41
- spec/**/*
42
- ].map {|pattern| Dir.glob(pattern)}.flatten.select{|f| File.file?(f)}
34
+ all_files_and_dirs = Dir.glob('**/*')
35
+ files_and_dirs_to_ignore = %w[pkg coverage doc].map {|dir| Dir.glob("#{dir}/**/*")}.flatten
36
+ files_and_dirs = all_files_and_dirs - files_and_dirs_to_ignore
37
+ files = files_and_dirs.select{|f| File.file?(f)}
43
38
  missing_files = files - spec.files
44
39
  extra_files = spec.files - files
45
- puts "Missing files:"
46
- puts missing_files.join(" ")
47
- puts "Extra files:"
48
- puts extra_files.join(" ")
49
- end
50
-
51
- desc 'Local install the latest gem version'
52
- task :reinstall do
53
- system("rm -f pkg/*.gem && rake gem && gem install pkg/*.gem")
40
+ unless missing_files.empty?
41
+ puts "Missing files:"
42
+ puts missing_files.join(" ")
43
+ end
44
+ unless extra_files.empty?
45
+ puts "Extra files:"
46
+ puts extra_files.join(" ")
47
+ end
54
48
  end
55
49
 
56
- desc 'Uninstall all Fresnel versions and install the latest gem version'
50
+ desc "Uninstall all #{spec.name} versions and install version #{spec.version}"
57
51
  task :upgrade do
58
- system("gem uninstall -a -x fresnel && rm -f pkg/*.gem && rake gem && gem install pkg/*.gem")
52
+ system("sudo gem uninstall -a -x #{spec.name} && rake gem && gem install pkg/#{spec.name}-#{spec.version}.gem")
59
53
  end
data/Readme.rdoc CHANGED
@@ -1,13 +1,39 @@
1
1
  = Permutations
2
2
 
3
3
  Generate permutations for nested arrays or strings that offer one or more choices.
4
- You can use it as a Rails plugin.
4
+
5
+ == Installation
6
+
7
+ You can use it as a Rails plugin or as a normal ruby.
8
+
9
+ script/plugin install git://github.com/Narnach/permutations.git
10
+
11
+ or
12
+
13
+ sudo gem install permutations
5
14
 
6
15
  == Example
7
16
 
17
+ You can permutate array elements:
18
+ require 'permutations'
8
19
  [[1,2],[3,4]].permutations # =>[[1,3],[1,4],[2,3],[2,4]]
20
+
21
+ You can also permutate choice sets extraced from strings. These choices are specified between curly braces:
9
22
  "{1,2}{3,4}".permutations # => ["13", "14", "23", "24"]
10
23
 
24
+ It is possible to use a custom separator, and whitespace surrounding options is automatically removed to allow for more reader-friendly formatting of the choicees:
25
+ "{ 1 | 2 }{ 3 | 4 }".permutations("|") # => ["13", "14", "23", "24"]
26
+
27
+ By default (custom) separators are regexp-escaped, but this can be turned off to use a regexp to determine the separation; for example when there are multiple separators:
28
+ "{1|2}{3,4}".permutations("[|,]", false) # => ["13", "14", "23", "24"]
29
+
30
+ == Contributing
31
+
32
+ Bugs and feature requests can be added in Lighthouse. Pull requests on github are welcome, too.
33
+ When you submit code, it is very much appreciated when have specs and perhaps a bit of documentation for new features.
34
+
35
+ https://narnach.lighthouseapp.com/projects/45010-permutations
36
+
11
37
  == Credits
12
38
 
13
39
  Created by Wes Oldenbeuving. Licensed under the MIT License.
data/lib/permutations.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Permutations
2
2
  module Array
3
3
  # Recursively permutate nested arrays.
4
+ # [[1,2],[3,4]].permutations # =>[[1,3],[1,4],[2,3],[2,4]]
4
5
  def permutations(leftovers=self)
5
6
  tail = leftovers.last
6
7
  return tail.map{|e| [e]} if leftovers.size == 1
@@ -15,11 +16,12 @@ module Permutations
15
16
  end
16
17
 
17
18
  module String
18
- # Create multi-level string permutations
19
- # "{a,b,c}{1,2,3}".permutations # => ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
19
+ # Create all possible string permutations of the available choice groups.
20
+ # "{a,b,c}{1,2,3}".permutations # => ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
20
21
  def permutations(separator=',', escape_separator=true)
21
22
  separator=Regexp.escape(separator) if escape_separator
22
23
  matches = self.scan(/(\{\s*(.*?)\s*\})/)
24
+ return [self] if matches.empty?
23
25
  substitutions = matches.map{|match| match[0]}
24
26
  permutations = matches.map{|match| match[1].split(/\s*#{separator}\s*/)}.permutations
25
27
  permutations.map do |permutation|
data/permutations.gemspec CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.name = 'permutations'
4
4
  s.summary = "Permutations is a library that allows you to generate permutations from Arrays and Strings."
5
5
  s.description = s.summary
6
- s.version = '0.1.2'
7
- s.date = '2010-01-21'
6
+ s.version = '0.1.3'
7
+ s.date = '2010-02-01'
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Wes Oldenbeuving"]
10
10
  s.email = "narnach@gmail.com"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permutations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wes Oldenbeuving
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-21 00:00:00 +01:00
12
+ date: 2010-02-01 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15