Category Archives: Ruby

Its the little things.

I’ve been spending time with Ruby a bit lately and the more I learn, the more I like it.

One of the simple things that I learned early on is that everything is an expression (including if statements).

That allows you to do things like this:

new_value = if some_flag then calculated_value else default_value end

That’s a bit contrived. You’d probably use the ternary operator instead, but you could have blocks of code or methods executed in each step.

Its not huge, but its still much better than the Java way of:

if (someFlag) {
	newValue = calculatedValue
} else {
	newValue = defaultValue
}

Another simple thing that is huge is that Ruby sees “nil” (aka “null” in Java) as being “false” you can do nil checks elegantly like this:

my_var = some_func_that_might_return_nil(val_to_calc) || "default"

The code will get the value of “default” if the function call returns nil. Again, in Java you’d have something like:

String myVar = someFuncThatMightReturnNull(valToCal);
if (myVar == null) {
	myVar = "default";
}

Again its subtle, but it reads and flows much better to me.

Obie responds

I posted a bit ago with a link to Zed’s blog about Rails being a ghetto. I found it really interesting, if a bit of an ego trip “I’m so important” post.

Well, Obie (a very vocal/public figure in the Ruby community) has responded to Zed’s rant. So for completeness: here it is

All of this is interesting to me as I try to figure out if Ruby (and Rails) is worth investing my limited free time into.

Cool things about Ruby

As I’m digging through the book “The Ruby Way“, here are several things that struck me as really cool (not sure how to make use of them yet, but..)

Swapping Variables:


X, Y = Y, X


Initializing Multiple Variables is very similar:


X, Y, Z = 1, 2, 3

Adding a method to an object instance:


str = "Hello World"

def str.enhance
self.to_s + " yabba dabba doo!"
end

puts str <== "Hello World" puts str.enhance <== "Hello World yabba dabba doo!"

Ruby allows you to add methods to object instances. The method I added "enhance" only exists on the instance variable "str". I'm not entirely sure how to make use of it yet, but it opens a whole new way of thinking about objects, classes, inheritance, etc putting a different twist on OOP.

Another handy thing is being able to easily create an array of strings:


months = %w(January February March)

Here's a simple, modified example from the book that (to me) shows some interesting things about Ruby:


print "Please enter the Temp and Scale (either C or F): "
entered_value = gets
exit if entered_value.nil? or entered_value.empty? #1
entered_value.chomp!
temp, scale = entered_value.split(" ") #2


In #1, you can put the "if" statement after the action and in this case I think its more readable.
In #2, multiple assignment is used to take the two strings from the split and assign them to variables.

For me, these few examples are part of what's piqued my interest in Ruby. Its nothing big to be sure, but I'm starting to see the potential to code a little differently.

The Ruby Way

Hey… in case you can’t tell one of my New Year’s resolutions is to blog a little more often. I’m doing good so far. 😉

Another one is to spend more time playing around with Ruby. I’ve been playing around with it for some time, but haven’t really gotten very deep with it. I’m trying to correct my lack of Ruby this year (that and learning Objective-C – but that’s for another post).

I own the famous Pick-Axe book and its pretty good (in fact I have both editions). But, to jumpstart my learning this year I picked up “The Ruby Way” by Hal Fulton. Although I’ve only gotten a little way through it, I’m pretty impressed with it. One thing in particular it does (better than the Pick-Axe book) is show you “rubyisms”. Little ways in which to take advantage of Ruby and teach you to not treat it like Java, C#, etc.

Check it out…

Piss and Vinegar

If you’re interested in Ruby on Rails you’ve got to check out this guy’s rant. I have no idea if he really knows what he’s talking about (he obviously thinks he does). Its an interesting post and he rips on two things I’ve thought highly of in the past (Ruby On Rails and ThoughtWorks). As they say… he’s full of piss and vinegar. So much for a “Happy New Year” for him. :-)

Rails 2.0 Is Coming

DHH has posted an overview of the features of Rails 2.0. Looking at Rails code is so nice. It conveys so much with so little. Rails is an amazing Domain-Specific-Languange for Web Development.

3rd Rail

Don’t know if you’ve seen this or not, but Borland…er… CodeGear has come out with their Rails IDE – 3rd Rail. I took a look at the 20 video overview and it looks pretty good (though the presenter is a little dry to listen too). Its obviously based on the Eclipse IDE (which isn’t a bad thing) and has some excellent HTML/CSS tools. I’m not sure if it’ll be the Rails IDE I’ve been waiting for, but its worth a closer look. Now… if only I could find a Rails gig…

Why haven’t you looked at Ruby on Rails?

You’re a Java developer and you haven’t tried Ruby on Rails? What’s wrong with you!?!?!? Come on out of the cave… you’ll get used to the light… trust me. :-)

OK.. I’m not saying “give up Java, Rails rulez!” No… but I think it is in the best interest of any Java developer to learn what Rails is about – the sooner the better. There is a lot that we can learn from Rails and it can show us where Java’s weaknesses are.


The good news is that you can get your feet wet with Rails significantly faster than you can with almost any significant Java framework (even from a seasoned Java developer’s standpoint – really!).

For example – Just in the time it takes you to:

  • download Spring, Hibernate, Tomcat, Equinox, HSQLDB
  • get a project setup within Eclipse

You could:

  • install a full Rails environment (Instant Rails or Locomotive)
  • create a web app that connects to and edits data from a table in a new database you create
  • start exploring/modifying the web app to do AJAX requests, do validation and change the look of the pages

My guess is that it would take you about an hour to get that far with Rails – an hour!

So, my suggestion to all of you sharp Java heads is start by buying “Agile Web Development with Rails”. Spend an hour or two reading the intro chapters. Then, work through the first step or two of their Depot application. Don’t worry about the details of the Ruby language, don’t worry knowing how Rails is working underneath. Focus just on what you can accomplish with the code you write as you work through the demo.

Give yourself about 4 hours total (plus the $40 to buy the book). That should be enough to give you a general impression of what Rails is and why its important. If it doesn’t impress you… check your pulse. 😉

In the end, I hope that you’ll either be convinced to spend more time with Rails (making it a bigger player in the world) or be inspired to help Java become better. Either way… life would be better for all of us.

Happy Coding