Published: 13 Aug 2008
By: Karl Seguin

Karl Seguin looks into ruby and gives us his opinion about it.

Ruby is a dynamic object oriented programming language which has found growing popularity amongst all types of developers. Shoes is a cross-platform framework for building desktop applications in Ruby. Shoes is particularly well suited as a Ruby-learning tool due to its simplicity and ability to quickly produce useful applications.

Contents [hide]

Introduction

Predictably, the Ruby hype from a year ago appears to have reached a more appropriate level. It's great that people are so passionate about languages, frameworks and tools, but, as always, people seem to lose common sense when talking about Microsoft's imminent death.

Ruby

Every developer should know a mix of languages, not only because some languages are better suited for some tasks, but also because there's no better way of growing as a programmer other than looking at programming through the eyes of a different community. I can trace most of my growth as a programmer from other languages, most notably (for me) PHP, Java and C++. Lately though, Ruby has been my favorite, largely because it makes learning and programming so fun. Ruby is a powerful dynamic language. It makes extensive use of advanced programming techniques, such as blocks and iterators, symbols, fluent interfaces and mixins - all of which are starting to show up in more traditional languages (such as C#).

Most of the hype around Ruby has been around the Rails framework - a powerful web framework known for rapid development. Rails highlights the benefit of different perspective quite well. If you've been doing ASP.NET development for the last 5 years, using webforms, DataSets and DataGrids, then you'll be blown away by Rails - it's as different from ASP.NET as ASP.NET is from classic ASP. Even if your all caught up with ASP.NET MVC the difference is still staggering. In some situation the Rails approach is better, in some it's worse. Of course, this isn't about being better or worse, it's about being different. When you step into your first Rails application, with their ActiveRecord implementation and Ruby's flexibility, it's like seeing your first piece of code ever - except you actually understand what's going on (at least that was my experience).

As nice as the Rails framework is, it isn't how I'd recommend you learn Ruby. The problem with Rails is that it's pretty complicated, and learning it while learning Ruby can be a pretty frustrating exercise.

Shoes

If you want to learn Ruby, and eventually Ruby On Rails, than I suggest you first start with Shoes. Shoes is a very simple cross-platform framework for desktop applications. The framework is so simple that you can focus on learning Ruby and still end up with useful applications. Before I give a quick example, there are two things that make Shoes an even better learning tool. First, it has a fair number of sample programs classified by complexity. Open up the 70-line simple-calc.rb and start learning. Even more impressive is the Shoes manual. Again, the manual offers a completely different perspective on traditional framework documentation. It's a bit like reading a bed time story, yet its effective as an introductory and reference guide.

The simple application that we're going to build is a Twitter reader. Type in the name of the Twitter account, and you'll get the latest posts. Here's a picture of what it'll look like

Figure 1: Our Shoes Application

Our Shoes Application

The only really tricky thing to learn about Shoes are its two layout controls: stacks and flows. Flows fill their content from left to right, while stacks go from top to bottom. To get our simple application up and running we'll need two stacks - the top one will have a label, textbox and button. The bottom one will be filled with content when the button is pressed. Here's the initial code that represents the main layout:

Listing 1: Our Application Shell

We've assigned the second stack to a variable called @content, so that we may add our RSS content to it later on. Next we'll fill the top part of our application. This must be within a flow so that the contents go from left to right:

Listing 2: Building the Top

As you can see, text is added via the para method, textboxes via the edit_line method, and buttons via the button method. All of these accept additional parameters, but we're just focusing on the basics (it's also worth pointing out that in Ruby parentheses are optional, so most of the Shoes code you are going to run across won't use them). The last step is to add the actual button code, here's the relevant section:

Listing 3: The missing Functionality

First the content is cleared, next we use Ruby's RSS class to parse twitter's RSS feed, and then for each item we add a paragraph to our @content stack.

It's a simple example, but it does demonstrate many Ruby constructs: the most obvious being the each block (ruby makes extensive use of blocks), and symbols such as :width and :height. If you've used C# and ASP.NET MVC, both of those should look quite familiar. Ruby should feel flattered - as they say imitation is the sincerest form of flattery.

Continue reading and see our next or previous articles

About Karl Seguin

Karl Seguin is an senior application developer at Fuel Industries, located in Ottawa, Ontario. He's an editor here at DotNetSlackers, a blogger for the influential CodeBetter.com and a Microsoft MVP.

This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.

You might also be interested in the following related blog posts


RadScheduler for Silverlight learning series, part 3: Add, Edit, and Delete Appointments read more
Serialising Microsoft StreamInsight QueryTemplates read more
Tech Ed 2009 Cometh - EF and more read more
A little visualization goes a long way read more
Using Visual Studio 2010 for Silverlight development read more
Functional Programming Battles GOTOzilla read more
Technology, Social Media seeping into my home read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 11: The Client-Only World read more
7 Stages of new language keyword grief read more
O# or The Fall Conference Speaking Schedule read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Nice introduction Xun Ding 8/13/2008 12:10 AM

Please login to rate or to leave a comment.