Reputation: 29514
I have a String like:
"/system/path/513/b02/36a/66ea2557[*,0].swf,16"
I'm using Ruby on Rails for my application. I am trying to make 16 urls by appending 1-16 to the end of 66ea2557 to get something like:
"/system/path/513/b02/36a/66ea25571.swf"
"/system/path/513/b02/36a/66ea25572.swf"
..
"/system/path/513/b02/36a/66ea255715.swf"
"/system/path/513/b02/36a/66ea255716.swf"
How can I do this in Ruby?
Upvotes: 0
Views: 235
Reputation: 16793
Assuming that:
"/system/path/513/b02/36a/66ea2557[*,0].swf,16"
from somewhere perhaps out of your control and it needs to be parsed16
at the end of the string won't necessarily be the same every time and signifies the number of urls you need[*,0]
part of the string for anything and it doesn't necessarily signify anythingI came up with:
string = "/system/path/513/b02/36a/66ea2557[*,0].swf,16"
base_path, _, file_extension, num_urls = string.split(/(\[\*,0\])|,/)
# => ["/system/path/513/b02/36a/66ea2557", "[*,0]", ".swf", "16"]
(1..num_urls.to_i).map { |i| "#{base_path}#{i}#{file_extension}" }
# => ["/system/path/513/b02/36a/66ea25571.swf",
"/system/path/513/b02/36a/66ea25572.swf",
...
"/system/path/513/b02/36a/66ea255716.swf"]
Upvotes: 2
Reputation: 794
You could use String#%:
path = "/system/path/513/b02/36a/66ea2557%s.swf"
(1..16).map {|index| path % index}
Upvotes: 0
Reputation: 3994
You could try simple looping. Something like:
#test.rb
arr = Array.new();
for i in 1..16
arr[i-1] = "/system/path/513/b02/36a/66ea2557" + i.to_s;
end
puts arr;
Upvotes: 0
Reputation: 8762
(1..16).each do |i|
puts "/system/path/513/b02/36a/66ea2557#{i}.swf"
end
Upvotes: 0