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.
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
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/.
> 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
Java, not supporting double-dispatch, can turn a pretty pattern like
Visitor into a mess.
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]
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).
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.
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/
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
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
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.
> 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
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 don't know, if you start throwing partition in there, you're this
close to just saying a.sort