/ / Manipolazione delle stringhe e concatenazione in Ruby on rails - ruby, string, ruby-on-rails-3.2

Manipolazione delle stringhe e concatenazione in Ruby su rotaie - rubino, stringa, ruby-on-rails-3.2

Ho una stringa come:

"/system/path/513/b02/36a/66ea2557[*,0].swf,16"

Sto usando Ruby on Rails per la mia applicazione. Sto cercando di creare 16 URL aggiungendo 1-16 alla fine di 66ea2557 per ottenere qualcosa del tipo:

"/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"

Come posso farlo in Ruby?

risposte:

2 per risposta № 1

Supponendo che:

  • Ti viene dato "/system/path/513/b02/36a/66ea2557[*,0].swf,16" da qualche parte forse fuori dal tuo controllo e deve essere analizzato
  • Il numero 16 alla fine della stringa non sarà necessariamente lo stesso ogni volta e indica il numero di URL necessari
  • Non hai bisogno del [*,0] parte della stringa per qualsiasi cosa e non significa necessariamente nulla

Mi è venuta in mente:

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"]

0 per risposta № 2
(1..16).each do |i|
puts "/system/path/513/b02/36a/66ea2557#{i}.swf"
end

0 per risposta № 3

Potresti provare un semplice loop. Qualcosa di simile a:

#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;

0 per risposta № 4

Potresti usare Corda#%:

path = "/system/path/513/b02/36a/66ea2557%s.swf"
(1..16).map {|index| path % index}