Ruby is fun
I spent my free time last weekend working on the my little app in "Ruby on Rails":http://www.rubyonrails.com/ . I'm only a novice programmer at best, so my experience at learning Ruby is likely different from someone that has another language down pat. But for me, Ruby is just fun. Part of it is the quality of learning I feel like I'm doing. I fee like the things I spent the most time on are things that ended up being one very brief line of code in the end, and also things that will apply to more than just that particular problem.
Like inject. That was fun. In the app I'm working on, it uses tags so each item has many tags, and I wanted to get a list of all the tags shown on the page. When I just added the arrays to a variable I got duplicates if the some of the ideas had the same tags. So I spent a decent part of the afternoon trying to figure out an intelligent way to not end up with duplicates. In the end, with some help from a friendly rubyist in the #rubyonrails, it ended up being:
@taglist = @items.inject([]) { |tags, item| tags|item.tags }
Inject is an iterator that takes a block and two variables, the first represents the current value of what will be returned by the iterator when it's done. The pipe character is Ruby's Union operator for Arrays, it combines two arrays, overwriting any duplicates.
I was thinking about the productivity of the hours I spent figuring that out. Some might consider it a waste to spend that much time on something that ends up being so short in length. But I feel like I actually learned something, something that I'll probably be able to apply in other places. And it's so simple and elegant in the end. It's funny how the number of lines of code has, in this instance, an inverse relationship to the amount of satisfaction I got out of discovering the answer. If had tackled the same problem in PHP, it's likely what I would have learned would've been just an ugly hack at best.
April 10th, 2005 - 01:17
Actually, wouldn’t that have just been handled by a call to array_unique() in PHP? I agree with you that Ruby (on Rails, particularly) is a lot nicer to develop on than PHP, but not always terribly intuitive (for me at least).
April 10th, 2005 - 15:00
That’s a good a point. array_unique() seems more procedural, but that touches on some of the reasons why I liked PHP. It’s very accessible and practical. I have this problem, ok here’s a function that solves it. With Rails I get to capitalize on the experience of a lot of other pretty sharp people. I think learning Ruby and Rails will help me write better code in other languages as well; PHP, Javascript. It is all a little nebulous at times though. I think once they “work on the documentation”:http://wiki.rubyonrails.com/rails/show/DocumentationDiscussion that could help improve things.
I think I also just like having better/more/different iterators.
April 13th, 2005 - 03:19
inject?
What’s wrong with Array#uniq or just using Set ?
Ruby has them too. You don’t need to use PHP for Array#uniq!
April 13th, 2005 - 14:38
My personal goal with this is to learn, not necessarily to get an application built. I guess I feel like with Ruby, well, I just find the stuff I’m learning more satisfying than with PHP. With PHP there’s so much code around that’s just procedural and, while it does the job, and learning it was satisfying, you don’t really learn as much about how to be a good programmer. I never even heard of things like unit testing and Model/View/Control separation when dealing with PHP. And I never ran across anything that tried such a high level approach to a framework as Rails. I never figured out how to use the command line to interactively test PHP. Does PHP have even have a breakpoint, IRB, object.inspect or object.debug, etc? These things have really helped me learn a lot about the code I’m writing.
I am a little jealous of my VB programmer friend’s Visual Studio IDE though. Maybe Ruby has something similar that I haven’t found yet.