Hello,
Can someone please help me to simply rename some files to lowercase in a
directory? I keep getting errors. Ruby literally renames the files to
the same filename, with an extension of “.lowercase!”
I’ve tried this:
File.rename("#{file}", “#{file}.downcase”)
and this:
File.rename("#{file", “#{file.downcase}”)
I need to do this because the files will end up on a Unix platform, and,
we’ve all agreed that they always be lower-case.
Hello,
Can someone please help me to simply rename some files to lowercase in a
directory? I keep getting errors. Ruby literally renames the files to
the same filename, with an extension of “.lowercase!”
I’ve tried this:
File.rename("#{file}", “#{file}.downcase”)
and this:
File.rename("#{file", “#{file.downcase}”)
I suggest you change ‘File.rename’ to ‘puts’ to see what’s going on.
That is, change your code to these:
puts “#{file}”, “#{file}.downcase”
puts “#{file}”, “#{file.downcase}”
Then find out the one which gives the result you expect. Then change it
back to File.rename.
Note that in this example, you don’t need to do any string substitution;
the value in ‘file’ is already a string, and file.downcase is also a
string. That is, you can also try:
puts file, file.downcase
Adding debugging output is one very simple way to debug problems. In
ruby this is often best done using p or inspect. This gives you the same
output display as you’d get in irb; for example, strings are surrounded
by double-quotes and have special characters escaped.
Hello,
Can someone please help me to simply rename some files to lowercase in a
directory? I keep getting errors. Ruby literally renames the files to
the same filename, with an extension of “.lowercase!”
The word ‘downcase’ is inside a string and outside any variable
interpolation, therefore it is just the letters ‘d’, ‘o’, ‘w’, ‘n’, ‘c’,
‘a’, ‘s’, ‘e’, not a method
call.
and this:
File.rename("#{file", “#{file.downcase}”)
…has 3 braces. Braces travel in pairs, so an odd number of braces is
not a good sign.
str = “HELLO”
puts “#{str.downcase} world”
–output:–
hello world
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.