Permutations is a library that allows you to generate permutations from Arrays and Strings.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Document interfaces with YARDoc Nov 23, 2011
spec Bugfix: support strings without permutations Feb 1, 2010
MIT-LICENSE First version Jan 21, 2010
Rakefile Readme changed to use markdown Nov 23, 2011
init.rb First version Jan 21, 2010
permutations.gemspec Version 0.1.4 Nov 23, 2011


Generate permutations for nested arrays or strings that offer one or more choices.


You can use it as a Rails plugin or as a normal ruby.

script/plugin install git://


sudo gem install permutations


You can permutate array elements:

require 'permutations'
[[1,2],[3,4]].permutations # =>[[1,3],[1,4],[2,3],[2,4]]

You can also permutate choice sets extraced from strings. These choices are specified between curly braces:

"{1,2}{3,4}".permutations # => ["13", "14", "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:

"{ 1 | 2 }{ 3 | 4 }".permutations("|") # => ["13", "14", "23", "24"]

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:

"{1|2}{3,4}".permutations("[|,]", false) # => ["13", "14", "23", "24"]


Bugs and feature requests can be added in Lighthouse. Pull requests on Github are welcome, too. When you submit code, it is very much appreciated when you have specs and perhaps a bit of documentation for new features.


Created by Wes Oldenbeuving. Licensed under the MIT License.