Third ruby script
I wanted to create a simple directory sort program for my FAT formatted media. So again forcing myself to learn Ruby I created yet another Ruby script. The algorithm is pretty simple.
- make a temporary directory
- move all the files in the temporary directory
- move all the files back to the original directory but in sorted order
- remove temporary directory
require "fileutils"
require "tmpdir"
file_list = Array.new
FileUtils.mkdir("tempsortfolder")
Dir.foreach(".") do |file_name|
if file_name =~ /\.erg/
file_list.push(file_name);
FileUtils.mv(file_name, "tempsortfolder");
end
end
file_list.sort.each do |file_name|
FileUtils.mv("tempsortfolder/#{file_name}", file_name);
end
FileUtils.rmdir("tempsortfolder")
I think I am getting a bit better at this Ruby stuff. Some of my observations on this last script that I wrote:
- I stopped using { } and kept everything as do and end which makes things look a bit more readable. However, I think that Ruby should only support one structure rather than both. And to be honest, I think I prefer { } since its less typing and C/C++/Perl/Java use the same construct.
- The require construct seems to load based on a file rather than an object name. This very ugly compared to the import in Java. I think it works the same way as Perl use and the C #include which are also just as bad. It means that along with knowing what class I want to use, I have to know which file it is in my big library folder. For one thing "tmpdir" contains "Dir" who would've thought that?
- The | | construct is quite odd to say the least. Ruby is the first language I have encountered with that construct. I still prefer a proper for statement, but the Ruby language moves such things as part of an object. The side effect of this I find is lack of consistency -- this is shown by arrays where I have to use each but for directories I have to use foreach.
- I haven't gotten into writing classes yet. So far none of the things I need require using it yet. Maybe Ruby's elegance will show there.

2 comments:
This snippet is another beautified version ;)
I noticed a few things about your script, and as a Rubyist I thought I might point some stuff out. Though your post is kind of old, so this all may be irrelevant, I still wanted to see if I could help you out.
1. the braces {} are quite helpful still. You don't need to stop using them. array.each { |a| puts a } you can do this all on one line instead of doing using the do and end statements.
2. Since I know some other language I can see how the require can be a bit taxing. The good thing though is you only have to do it once, and since it's the file name it can be either what ever you want it to be i.e. the object you are using or some ruby class that doesn't change. As of this post it looks like you haven't got into ruby classes yet, but generally you want 1 class to be in each file unless the classes are closely related or something like that. I see that more in Rails than anything.
3. you can use a for statement in ruby like for product in products and so on.
4. You don't have to require "tmpdir" just to use the Dir class. it's already available to you.
Happy programming.
Post a Comment