Skip to content

Given a string containing placeholders (like [color]), enumerate all of the possible strings resulting from filling those placeholders with replacements (like red, blue).

Notifications You must be signed in to change notification settings

seamusabshere/string_enumerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

string_enumerator

You provide a list of replacements (“replace [color] with red and blue”) and then call the #enumerate method on a String:

?> u = StringEnumerator.new(:color => [ 'red', 'blue' ])
=> #<StringEnumerator...>
?> u.enumerate 'http://example.com/[color]'
=> [ 'http://example.com/blue', 'http://example.com/red' ]

The start and end of placeholders are marked with “[” and “]” (although you can override this if you want, see the tests.)

Multiple placeholders per string

You can define as many replacements as you want, which will exponentially increase the final number of enumerations

?> u2 = StringEnumerator.new(:color => [ 'red', 'blue' ], :taste => [ 'savory', 'sweet' ])
=> #<StringEnumerator...>
?> u2.enumerate 'http://example.com/[color]/[taste]'
=> [ 'http://example.com/blue/savory', 'http://example.com/blue/sweet', 'http://example.com/red/savory', 'http://example.com/red/sweet' ]

Thanks to

Copyright 2011 Seamus Abshere

About

Given a string containing placeholders (like [color]), enumerate all of the possible strings resulting from filling those placeholders with replacements (like red, blue).

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages