Forum: Ruby ruby string function

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nilesh K. (Guest)
on 2009-02-14 07:51
hi


i am writing code in ruby which for converting

amjad_alI_khAn\khamAj-4.amjad_alI_khAn.zakir_hussain_and_the_maestros_3.mp3

format from file to

  Zakir Hussain And The Maestros 3\Khamaj 4 - Amjad Ali K. (Zakir
Hussain And The Maestros 3).mp3

such as
1)remove _ and replace by space
2)make first letter capital of each letter

 and store this format in another file
can anyone help me
7stud -. (Guest)
on 2009-02-14 09:08
Nilesh K. wrote:
> hi
>
>
> i am writing code in ruby which for converting
>
> amjad_alI_khAn\khamAj-4.amjad_alI_khAn.zakir_hussain_and_the_maestros_3.mp3
>
> format from file to
>
>   Zakir Hussain And The Maestros 3\Khamaj 4 - Amjad Ali K. (Zakir
> Hussain And The Maestros 3).mp3
>
> such as
> 1)remove _ and replace by space
> 2)make first letter capital of each letter
>
>  and store this format in another file
> can anyone help me

str =
'amjad_alI_khAn\khamAj-4.amjad_alI_khAn.zakir_hussain_and_the_maestros_3.mp3'

new_str = str.gsub("_", " ")
puts new_str

new_str = new_str.gsub(/\w*/) do |match|
  match.capitalize
end
puts new_str

new_str = new_str.gsub("-", " ")
puts new_str

pieces = new_str.split(/\.|\\/)  #split on . or \
p pieces

result1 = pieces[3]
result2 = pieces[1]
result3 = pieces[2]
result4 = pieces[3]
result5 = pieces[4].downcase

puts "#{result1}\\#{result2} - #{result3} (#{result4}).#{result5}"

--output:--
amjad alI khAn\khamAj-4.amjad alI khAn.zakir hussain and the maestros
3.mp3
Amjad Ali K.\Khamaj-4.Amjad Ali K..Zakir Hussain And The Maestros
3.Mp3
Amjad Ali K.\Khamaj 4.Amjad Ali K..Zakir Hussain And The Maestros
3.Mp3
["Amjad Ali K.", "Khamaj 4", "Amjad Ali K.", "Zakir Hussain And The
Maestros 3", "Mp3"]
Zakir Hussain And The Maestros 3\Khamaj 4 - Amjad Ali K. (Zakir
Hussain And The Maestros 3).mp3
This topic is locked and can not be replied to.