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.
C47e75cdd405c006fb3a3a0cae147302?d=identicon&s=25 Nilesh Kulkarni (nilk_cse)
on 2009-02-14 06: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 Khan (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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-14 08:08
Nilesh Kulkarni 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 Khan (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 Khan\Khamaj-4.Amjad Ali Khan.Zakir Hussain And The Maestros
3.Mp3
Amjad Ali Khan\Khamaj 4.Amjad Ali Khan.Zakir Hussain And The Maestros
3.Mp3
["Amjad Ali Khan", "Khamaj 4", "Amjad Ali Khan", "Zakir Hussain And The
Maestros 3", "Mp3"]
Zakir Hussain And The Maestros 3\Khamaj 4 - Amjad Ali Khan (Zakir
Hussain And The Maestros 3).mp3
This topic is locked and can not be replied to.