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.
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.
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;
public static void main(String args) throws Exception {
ServerSocket server = new ServerSocket(12345);
while (true) {
new TellTime(server.accept()).start();
}
}
}
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:
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.
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...
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"
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
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.
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.
I don't know, if you start throwing partition in there, you're this close to just saying a.sort
···
On Dec 6, 2005, at 12:47 PM, gabriele renzi wrote:
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
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.
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).
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.
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.