Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Good Ruby Examples?

5 views
Skip to first unread message

Hampton

unread,
Dec 1, 2005, 9:12:22 AM12/1/05
to
I'm planning on doing a tutorial about Ruby for Ryerson University's CS
Majors group. No one here knows anything about Ruby, so I thought it'd
be a great way to evangelize a bit. I'm always surprised how few
students actually code at all on their own. Most of them know Java and
C from class, but find them to be nothing more than class projects.

What I'm asking you guys for is if you have any short code ideas that
can be programmed *really* quickly in Ruby that might illustrate its
versatility. Some code example that would be hell to write in other
languages that would be interesting for an hour long explaination. I'd
say about 20 to 30 lines or so.

So, what's everybody's favorite smallish examples of the power of
coding with Ruby.

-hampton.

Lou Vanek

unread,
Dec 1, 2005, 10:14:36 AM12/1/05
to
C has 'Hello world!'
Ruby has the proverbial 'ToDo' list,
http://manuals.rubyonrails.com/read/book/7

James Britt

unread,
Dec 1, 2005, 10:57:36 AM12/1/05
to
Lou Vanek wrote:
> C has 'Hello world!'
> Ruby has the proverbial 'ToDo' list,
> http://manuals.rubyonrails.com/read/book/7

Except the typical C "Hello, world" example doesn't start with, "First,
download and install a set of libraries that does all the interesting bits."

For a small example, show the use of blocks. Give a Rake demo, then
examine the Rake lib code that mkaes it possible.

Maybe go through the code of Jim Weirich's Builder library, or something
else that shows the power of method_missing and dynamic code invocation.


James

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools


Jules Jacobs

unread,
Dec 1, 2005, 11:19:17 AM12/1/05
to
If you want to show them how fun ruby is: start with a c/java program
and translate it to ruby step-by-step.
Like this one:
http://onestepback.org/articles/groovy/ajavaprogram.html

Maybe something like:

a = false
puts ('aa'..'zz').select{a = !a}

prints:
aa
ac
ae
etc.

Will be huge in java ;-)

This can be an introduction, but you need a practical example too.

--
Posted via http://www.ruby-forum.com/.


James Edward Gray II

unread,
Dec 1, 2005, 12:00:32 PM12/1/05
to
On Dec 1, 2005, at 9:57 AM, James Britt wrote:

> Lou Vanek wrote:
>> C has 'Hello world!'
>> Ruby has the proverbial 'ToDo' list,
>> http://manuals.rubyonrails.com/read/book/7
>
> Except the typical C "Hello, world" example doesn't start with,
> "First, download and install a set of libraries that does all the
> interesting bits."

I agree. Avoid introducing 50 great add-on tools and target language
basics.

> For a small example, show the use of blocks.

Amen.

I was reading a Java book this morning that talked about the Visitor
Design Pattern (pretty block-like) as the root of all evil in the
world. I actually got mad reading the text. Now I realize, they
just don't "get it"...

> Give a Rake demo...

Wait, didn't we just get back to downloading libraries? ;) (I'm
kidding around here. It's a fine idea.)

Perhaps there's a decent topic, or just some inspiration, hiding in
the Ruby Quizzes (http://rubyquiz.com/). Of course, we know I'm
biases there.

James Edward Gray II

curtis.s...@gmail.com

unread,
Dec 1, 2005, 12:06:31 PM12/1/05
to

>
> I was reading a Java book this morning that talked about the Visitor
> Design Pattern (pretty block-like) as the root of all evil in the
> world. I actually got mad reading the text. Now I realize, they
> just don't "get it"...

Java, not supporting double-dispatch, can turn a pretty pattern like
Visitor into a mess.

Lou Vanek

unread,
Dec 1, 2005, 12:15:14 PM12/1/05
to
James Britt wrote:
> Lou Vanek wrote:
>
>> C has 'Hello world!'
>> Ruby has the proverbial 'ToDo' list,
>> http://manuals.rubyonrails.com/read/book/7
>
>
> Except the typical C "Hello, world" example doesn't start with, "First,
> download and install a set of libraries that does all the interesting
> bits."

Good point. You get to show what a rich set of libraries Ruby has and
how easy libraries can be installed with just one command. Thanks for
pointing that out.

-lv


[snip]

James Britt

unread,
Dec 1, 2005, 12:32:23 PM12/1/05
to
James Edward Gray II wrote:
> On Dec 1, 2005, at 9:57 AM, James Britt wrote:
>
>> Give a Rake demo...
>
>
> Wait, didn't we just get back to downloading libraries? ;) (I'm
> kidding around here. It's a fine idea.)
>

The idea is to give a Rake demo, and then examine the Rake library code
itself as the real example of Ruby. No hand waving (which a Rake demo
alone would entail); show how Ruby lends itself to robust, high-level
abstractions (such as Rake tasks).

James Britt

unread,
Dec 1, 2005, 12:37:34 PM12/1/05
to

A valuable aspect of Ruby and gems, but it says nothing about the
language per se.

I'd be wary of emphasizing the "rich set of libraries" aspect, too, as
Python, Perl, and Java probably have Ruby beat.

Better to show how much one can do, and how easily, without extra libraries.

Ruby's biggest selling point is Ruby itself.

Martin Ankerl

unread,
Dec 1, 2005, 12:53:56 PM12/1/05
to
> So, what's everybody's favorite smallish examples of the power of
> coding with Ruby.

This is my favourite, here are two solutions for the task
"Write a threaded server that offers the time"
(Ruby example is taken from the book 'The Ruby Way'):


# Ruby
require "socket"

server = TCPServer.new(12345)

while (session = server.accept)
Thread.new(session) do |my_session|
my_session.puts Time.new
my_session.close
end
end


// And the functional equivalent in Java:
package at.martinus;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class TimeServer {
private static class TellTime extends Thread {
private Socket soc;

public TellTime(Socket soc) {
super();
this.soc = soc;
}

public void run() {
try {
this.soc.getOutputStream().write(new Date().toString().getBytes());
} catch (Exception e) {
} finally {
try {
this.soc.close();
} catch (IOException e1) {
}
}
}
}

public static void main(String args[]) throws Exception {
ServerSocket server = new ServerSocket(12345);
while (true) {
new TellTime(server.accept()).start();
}
}
}


--
martinus | http://martinus.geekisp.com/

Chris McMahon

unread,
Dec 1, 2005, 3:39:19 PM12/1/05
to
I'm not sure what level your students are, but a demo of drivting
Internet Explorer with the Watir project code
(http://wtr.rubyforge.org) is colorful and fun. Advanced students can
investigate the Watir libraries and discuss Domain Specific Languages;
less advanced students can investigate the Watir unit tests and
examples quite easily. For instance, the code below is from the Watir
distribution and tests Google Maps:

require 'watir'
include Watir
require 'test/unit'

class TC_google_maps < Test::Unit::TestCase

def test_google_maps

testSite = "http://maps.google.com"
ie = IE.new
ie.goto(testSite)
ie.text_field(:id,"q").set("Durango,CO")
ie.button(:index, 1).click

matchlat = '37.275278'
matchlong = '-107.879444'

assert_match(matchlat,ie.frame("vp").html.to_s)
assert_match(matchlong,ie.frame("vp").html.to_s)
end
end

Ryan Leavengood

unread,
Dec 1, 2005, 4:56:04 PM12/1/05
to
How about an application which uses open-uri or Net::HTTP to download
all the files of a particular type from a web-page? For example, all
the images, or all the linked images, or all the linked mp3 files, or
whatever. I'd recommend just using regular expressions for the HTML
parsing, since that will show the power of regular expressions and
will also avoid the potential of Ruby XML parsing libraries barfing on
bad HTML.

The URI library can be used to handle relative URLs, etc.

This will be a good demo, and useful too.

Another option is a bulk-downloader that takes special URLs like
http://www.somesite.com/images/image_[01-25].jpg to get all the images
from image_01.jpg to image_25.jpg.

Ryan


Hampton

unread,
Dec 1, 2005, 5:07:11 PM12/1/05
to
James, that's more what I'm thinking about.

Less libraries, and more how powerful its paradigms are. Especially its
unique syntax and how readable it is (when done right).

Thanks everyone for your suggestions though. They are very good.

Alan Chen

unread,
Dec 1, 2005, 7:00:10 PM12/1/05
to
Since the audience is composed of CS majors, some classic algorithms in
ruby vs java or C might be in order. Also you might show them the
buildup of a multi-level data structure, then dump and retrieve using
yaml...

Paul

unread,
Dec 1, 2005, 9:13:23 PM12/1/05
to
Show them the basics of the language, ALA "Ruby in a Nutshell".
Let them loose with irb & ri.

Wayne Vucenic

unread,
Dec 6, 2005, 12:42:44 AM12/6/05
to
Hi Hampton,

> What I'm asking you guys for is if you have any short code ideas that
> can be programmed *really* quickly in Ruby that might illustrate its
> versatility. Some code example that would be hell to write in other
> languages that would be interesting for an hour long explaination.

My favorite is the well-known quicksort algorithm:

def quicksort(a)
return a if a.size <= 1
pivot = a[0]
quicksort(a.select {|i| i < pivot }) +
a.select {|i| i == pivot } +
quicksort(a.select {|i| i > pivot })
end

quicksort is "hell" to write in C or C++ unless you're extremely clever.

Wayne Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"


Han Holl

unread,
Dec 6, 2005, 10:16:06 AM12/6/05
to
On 12/6/05, Wayne Vucenic <night...@gmail.com> wrote:
>
> Hi Hampton,

>
>
> My favorite is the well-known quicksort algorithm:
>
> def quicksort(a)
> return a if a.size <= 1
> pivot = a[0]
> quicksort(a.select {|i| i < pivot }) +
> a.select {|i| i == pivot } +
> quicksort(a.select {|i| i > pivot })
> end
>
> quicksort is "hell" to write in C or C++ unless you're extremely clever.
>
> Wayne Vucenic
> No Bugs Software
> "Ruby and C++ Agile Contract Programming in Silicon Valley"
>
>
And then demonstrate how this actually works with all kinds of objects that
implement the comparison operators .
Nice duck typing example.

Han

gabriele renzi

unread,
Dec 6, 2005, 12:44:05 PM12/6/05
to
Wayne Vucenic ha scritto:

> Hi Hampton,
>
>
>>What I'm asking you guys for is if you have any short code ideas that
>>can be programmed *really* quickly in Ruby that might illustrate its
>>versatility. Some code example that would be hell to write in other
>>languages that would be interesting for an hour long explaination.
>
>
> My favorite is the well-known quicksort algorithm:
>
> def quicksort(a)
> return a if a.size <= 1
> pivot = a[0]
> quicksort(a.select {|i| i < pivot }) +
> a.select {|i| i == pivot } +
> quicksort(a.select {|i| i > pivot })
> end
>
> quicksort is "hell" to write in C or C++ unless you're extremely clever.

you may appreciate Enumerable#partition:
def qs(a)
return a if a.size <=1
pivot = a.shift
less, more = a.partition{|y| y < pivot}
qs(less) + [pivot] + qs(more)
end


Logan Capaldo

unread,
Dec 7, 2005, 12:00:02 AM12/7/05
to

I don't know, if you start throwing partition in there, you're this
close to just saying a.sort

0 new messages