Ruby: Turn bash.org quotes into a fortune file

The following code turns XML quotes from bash.org (a popular IRC quotes server) into a fortune-cookie file.
Handy for using with ../emacs/flashcard.el and my ../emacs/flashcard-config.el, which pops up a fortune
every time I get a correct answer.

bash-org-to-fortune.rb:

require 'rss/1.0'
require 'cgi'
require 'net/http'
host = Net::HTTP.new('bash.org', 80)
if ARGV[0] then
   resp, data = host.get('http://bash.org/xml/?top&below=' + ARGV[0], nil)
else
   resp, data = host.get('http://bash.org/xml/?top', nil)
end
parsed = RSS::Parser.parse(data, false)
parsed.items.each { |x| puts CGI::unescapeHTML(x.description.gsub('
', "\n")); puts "%\n" }

Call like this:

ruby bash-org-to-fortune.rb > bash; strfile bash; fortune bash

# or to get the top quotes with score < 1000
ruby bash-org-to-fortune.rb 1000 > bash; strfile bash; fortune bash

On Technorati: , ,

Congrats to teams who competed in Java Cup and Quizzardry!

Ealden wrote:

Congratulations to the teams who competed in the recently concluded
Java Cup and Quizzardry competitions! The Java Cup was held last 3
Mar 2005, while the Quizzardly was held the next day, 4 Mar 2005. The
teams competed from those sent by DLSU, iAcademy, UPD, AMA, FEU, Chang
Kai Shek, and UA&P.

The said events were part of the 20th Computer Science Week of the
University of the Philippines, Diliman, and were both organized by the
UP Association of Computer Science Majors (UP CURSOR).

JAVA CUP

Champion: Ateneo 3

  • Miguel Arguelles (4 CS)
  • Allan Espinosa (3 ECE)
  • Michael Tan (2 CS, alternate)

Second runner up: Ateneo 2

  • Joseph Noel (4 CS)
  • Paul Rivera (4 CS)

4th place: Ateneo 1

  • Erik Uy (4 CS)
  • Charles Yeung (4 MIS)
  • Ramon “Akie” Mejia (3 CS, alternate)

QUIZZARDRY

Champion: Ateneo 2

  • Mark Punzalan (3 CS)
  • Vanessa Gonzales (3 CS)
  • Randolph “Zippy” Espinosa (3 CS)

Second runner up: Ateneo 3

  • Erik Uy (4 CS)
  • Marc Lihan (3 CS)
  • Erwin Lee (3 MIS)

Ateneo 1

  • Ealden Escanan (3 MIS)
  • Gil San Pedro (3 CS)
  • Robin Yu (1 CS)

Pictures: http://www.engg.upd.edu.ph/%7Ejoyce/csweek/index.html ;)

Congrats! :D

Ealden Esto E. Escañan
Outgoing CompSAt President

E-Mail from Ealden Esto E. Escañan

On Technorati: , ,

Common networking errors

Due 2004.02.23 11:59:59 PM: Source code and detailed notes on

– changes made
– problems encountered
– solutions tried/found
– outstanding problems
– plans for next step

I don’t expect everyone to have a fully-working game by tomorrow, so
most of your partial points will come from the notes. “It doesn’t
work.” isn’t enough; I need to know why it doesn’t work, if it used to
work before, what you’ve tried doing to get it to work, why you think
it doesn’t work, etc.

Along the way to your first milestone, you may have encountered the
following problems:

ObjectInputStream seems to hang when you create it.

That happens when both sides of the connection try to create object
input streams at the same time, but the other side hasn’t opened an
object output stream yet. Fix: create the object output stream first
before you create the input stream.

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

Your program lags terribly.

How are you doing your networking and repainting code? If you’re doing
animation, repaint() should be in one thread and your networking code
should be in another. You shouldn’t do them in the same thread because
then repaint() will only happen whenever something is received from
the socket connection. Also, make sure you have delays somewhere!
while (true) { repaint(); } means your CPU will spend
most of its time repainting. Add a Thread.sleep in that loop so that
your computer has time to do other things.

You can’t write certain objects over the stream.

Make sure the object is serializable and all of its attributes are
either transient or serializable as well. You really shouldn’t be
sending your entire application over the network – what is the other
side going to do with all those textfields? Mark many of your
attributes transient or create a small, simple object that
encapsulates the data you need to send.

You’ve sent an object over the network connection and you’re calling methods on it on the other side, but the one on the first computer isn’t changing!

That’s the way it works. If you send an object from computer A to
computer B, computer B isn’t working on the original object but on a
copy of it. Any changes on A won’t be automatically sent to B and vice
versa. The correct thing to do is examine the object and perform
different actions locally.

If you really need to get something like this working and you have
plenty of spare time, read up on RMI (Remote Method Invocation). It’s
out of the scope of CS21B, though.

For tonight, I want lots and lots of notes.

TOMORROW: Data structures (stacks, queues and linked lists).