Skip to content

Instantly share code, notes, and snippets.

@gilles
Created November 9, 2010 23:35
Show Gist options
  • Save gilles/670048 to your computer and use it in GitHub Desktop.
Save gilles/670048 to your computer and use it in GitHub Desktop.
bash like brace expansion
# bash like brace expansion
#
# {1,2}.3.4.{5,6}.7.8.{9,10} becomes
# [1.3.4.5.7.8.9,
# 2.3.4.5.7.8.9,
# 1.3.4.6.7.8.9,
# 2.3.4.6.7.8.9,
# 1.3.4.5.7.8.10,
# 2.3.4.5.7.8.10,
# 1.3.4.6.7.8.10,
# 2.3.4.6.7.8.10]
#
# to use with things that don't like cmd line params like Rake.
# For info this is how to pass params to a rake task (and you don't want to use ENV)
# rake task_name[param1,param2]
#
# @param [String] the string to expand
# @return [Array] an array of String
def expand(string, delim=',')
#check for brace expansions
res = ['']
string.split(/\{(.*?)\}/).each do |part|
if part.include?(delim)
#expansion
new_res = []
part.split(delim).each do |sub|
res.each do |r|
new_res << r+sub
end
end
res = new_res
else
#constant
res.each do |r|
r << part
end
end
end
res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment