programming ruby
yeah, i’ve finally started using ruby. instead of diving into the depths of ruby on rails i decided to start with something really easy:
i had written a simple tool in java that gathers a couple of text files from the file system, does a bit of parsing and creates an index html file as well as copies those text files to a new location. it consists of 4 classes:
- main class: gathers a list of files to start with and uses the other classes to do the rest - 47 lines
- SceneOutput - this class parses a text file to read some descriptions and that - 137 lines
- HtmlGenerator - generates the html file from a set of SceneOutut objects - 131 lines
- FileHelper - methods for copying files - 82 lines
java lines: 397
i then reimplemented the whole thing using ruby (don’t ask me why). the ruby implementation consists of the same classes minus the FileHelper, simply because ruby has built in file copying (even recursive directory trees)
- main - 22 lines
- sceneOutput - 39 lines
- htmlgenerator - 65 lines
ruby total: 126
well, what can i say. it’s obvious that the ruby solution uses way less code to achieve the same results. since that was my first playing around with ruby it wasn’t really faster than doing it in java, but i’m optimistic this will improve quite a lot.
for now just a list of my personal pros/cons for/against ruby:
pros
- less code, e.g. you can implement all getters of a class in one line:
attr_reader :property1, :property2 ... - more expressive language: instead of just an if clause in java, in ruby you have also unless (not if), which can also be written after the actuall statement, elsif statements etc.
- blocks - instead of having to implement certain interfaces all the time you can simply pass a piece of code to a method using a block
- less verbose - you simply don’t have to type that much, the apis are simpler to use, e.g. reading file content:
java:FileInputStream stream = new FileInputStream("xx.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
while((line = reader.readLine()) != null) {
doSomething();
}
stream.close();
ruby:
file = File.open("xx.txt", "r")
file.each_line {|line|
do_something
}
- more to come
cons
- there’s no cool IDE like eclipse (or i simply don’t know any), i actually used the RDT eclipse plugin, which brings some very basic syntac checking, colored code, an outline view and a button to run ruby from within eclipse. no code completion, no documentation tooltips, no linking between classes/methods, no error checking in the editor…
- no private methods - everything in a ruby class is public (at least i think it is, update: thanks to a comment i know better now, there are private and protected methods), confusing….
- haven’t come across something like javadoc yet, but i think i’ve heard of something like it somewhere
- no classloader.getResourceAsStream to load resources (the css file)
- the sytax is more complex than the java stuff, but i got used to it
- i wonder if there’s something like hibernate for ruby
- probably some more things to come, but not many
conclusion
i’m not sure why but it was real fun do code in ruby, everything just runs more smoothly, is easier and faster to achieve. if there was an ide like eclipse that could mark my typos and missing commas it would be even better.
i think i’ll try to use it as often as i can from now on. and i’ll delete that java version of the tool….
btw, i’m gathering links i find useful on my del.icio.us linklist
Tags: