Tibs Manor

Tuesday, October 11, 2005

Ruby

After seeing one too many posts on various blogs about the programming language Ruby or one of its related topics like Ruby on Rails, I decided it was high time I learned a bit about the language. After running through a basic tutorial on control flow, arrays/hashes, methods/classes, etc., I have a decent start on things. All I need is an actual idea for a program to learn on, it's hard for me to really get a sense of a language until I have a project that needs doing. If any readers have a desired program, please let me know, and maybe I can write it for you. Otherwise maybe I'll use it to drive my LCD screen now that WBCH is no more.


The hype about no exceptions (I mean special cases, not error handling - Ruby definitely has error handling) seems a little overblown. Integers are objects, unlike the primitive type/object distinction in older versions of Java (I'm talking about pre-generics), but you can't do Integer.new constructors the way you can with other classes in Ruby, like String.new. I'm sure there are other issues like that, but 9 pages into a very basic tutorial I would have hoped to find none! Also, I don't like the way the + operator handles different operand types, even if it is logical:

puts "this is a test"
puts (5+1)
"add a string to " + 4
puts "here's a six: " + (5+1)
# works
# works
# fails, eh so what
# fails, but you do this sort of thing often!

The solution is to explicitly convert the integer (5+1) to a string:

puts "this is the number six: " + (5+1).to_s

To me, better behavior would be if the + operator, when confronted with mixed, uncastable data types, defaulted to concatenation. I can understand that if presented with "5" + 5, the interpreter can't rightly decide if the programmer is expecting "55" or 10...but if the string is unconvertable to a number, concatenation seems like a safe choice. Maybe there's a good reason for this that I haven't encountered yet, and maybe there's a good workaround, but for now it just seems a bit annoying.


What I do like is the same thing that the author of the tutorial is most excited about, and that's the way it handles what amount to function pointers. Not that Ruby is the only language where this is possible, I know you can do this sort of thing in PHP, I've written custom code snippets that can be passed to an array sort method. You can kind of jump through hoops and get some similar behavior with Java but it looks a lot cleaner and simpler in Ruby.


Anyway not sure why this is superior to Java or Python or any other language but it is always good to learn something new. Hopefully I can come up with a project for this, that should give me a better sense of how I feel about it.

1 Comments:

  • Function pointers? You want these? Please explain why. I am naive on this one.

    By Blogger Shawn, at 9:45 AM  

Post a Comment

<< Home