Skip Links

The beauty of Ruby

By Mark Gibbs, Network World
December 20, 2004 12:13 AM ET
Gibbs
  • Print

To the tune of Jingle Bells:

Dashing through the code, without a spec in sight,
O'er the bugs we go, coding with all our might.
Breakpoints fill the source, as we try to get it right
Oh, what fun to debug code even on Christmas night.
Oh, Jingle bells, COBOL's hell, C's an awful pain
If I didn't have Ruby, I think I'd go insane.
Oh ...

That's enough festive nonsense for one season. Last week we promised a quick tour of the Ruby language so here goes: First, re-read last week's Gearhead for the background. It's OK, we'll wait for you. . . . Right, now you're back, here's a Ruby script:

def add_jingle(text)
return line = "Jingle " + text
end
%w[bells, bells, all the way].each {|name| puts add_jingle(name)}

We're sure you can figure out what the output of this program would be but let us point out some interesting features: First, we define a method, add_jingle (remember, Ruby is a completely object-oriented language).

Next, the %w, that's a shortcut for creating arrays that lets you leave the quotes and commas out. The each method (called an "iterator") applied to the array extracts each element from the array, and the code in braces is a "block" that is associated with the each method and gets executed once for each element.

Parameters are passed to blocks through variables identified by being bracketed by the bar symbol and that follow immediately after the braces. We could have written the program more concisely:

%w[bells, bells, all the way].each {|name| puts return line = "Jingle " + name)}

This is more subtle than it looks, as it involves a sort of recursive call to the method using the block as an argument.

If this all sounds a little convoluted we suggest you buy a copy of Programming Ruby, The Pragmatic Programmer's Guide, Second Edition, by Dave Thomas (published by The Pragmatic Bookshelf).

This is an excellent book. It introduces all the features of Ruby in an orderly, organized way and provides an exhaustive reference to the language. One of the great attributes of this text is readability - it really simplifies coming to grips with this rich, complex language.

You want to get an idea of what Ruby can do with TCP and threading? Check this out:

require 'net/http'
pages = %w( www.rubycentral.com

www.awl.com
www.pragmaticprogrammer.com
)
threads = []
for page in pages
threads << Thread.new(page) { |myPage|
h = Net::HTTP.new(myPage, 80)
puts "Fetching: #{myPage}"
resp, data = h.get('/', nil )
puts "Got #{myPage}: #{resp.message}"
}
end
threads.each { |aThread| aThread.join }

When we ran this example, we got:

Fetching: www.rubycentral.comFetching: www.awl.com
Fetching: www.pragmaticprogrammer.com
Got www.awl.com: Moved Temporarily
Got www.rubycentral.com: OK
Got www.pragmaticprogrammer.com: OK

As you can see, we started a new thread for each fetch. And each acted, as it should, independently, outputting its result as each thread retrieved the targeted contents. We can't explain why there was no line feed before the second "Fetching" on the first output line - let us know if you have any ideas. We tried adding line feeds but no joy.

  • Print
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Videos

rssRss Feed