Here is a short comparison between Ruby and PHP. The task was to print the location of all HTML links in a webpage using regular expressions.
If anyone knows of a cleaner or more efficient way to do this in Ruby or PHP, please post it in the comments!
Ruby
require 'net/http'
#connect and get the webpage
host = Net::HTTP.new('www.site.com.au', 80)
body = host.get('/index.php', nil ).body
puts "Links found..."
#find link URIs
links = body.scan(/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/)
#print all link URIs
links.each {|id,uri| puts uri}
PHP
<?php
$page = file_get_contents('http://www.site.com.au/index.php');
// find links
preg_match_all('/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/', $page, $links);
// links found
foreach($links[2] as $link)
{
print "$linkn";
}
?>
I’ve just started reading Why’s (Poignant) guide to Ruby.
The writing style is a little different to most technical books but that’s what makes it interesting.
Here’s an example from chapter 1…
One day I was walking down one of those busy roads covered with car dealerships (this was shortly after my wedding was called off) and I found an orphaned dog on the road. A wooly, black dog with greenish red eyes. I was kind of feeling like an orphan myself, so I took a couple balloons that were tied to a pole at the dealership and I relocated them to the dog’s collar. Then, I decided he would be my dog. I named him Bigelow. We set off to get some Milkbones for Bigelow and, afterwards, head over to my place, where we could sit in recliners and listen to Gorky’s Zygotic Mynci. Oh, and we’d also need to stop by a thrift store and get Bigelow his own recliner.
I thought the author (Why) did a great job of explaining global variables to beginners…
Some parts of your program are like little houses. You walk in and they have their own variables. In one house, you may have a dad that represents Archie, a travelling salesman and skeleton collector. In another house, dad could represent Peter, a lion tamer with a great love for flannel. Each house has its own meaning for dad.
With global variables, you can be guaranteed that the variable is the same in every little house.
Since I’m fairly new to Ruby, I thought it would be a good idea to give myself a trivial programming task.
I decided to create a simple script to generate a star field typical of the DOS gaming era.
To make things a little more challenging, I used a grid to make sure stars were evenly spaced and covered the whole image.
Here’s the code:
require 'rubygems'
require 'RMagick'
include Magick
#create the sky
$sky = Image.new(200,200){ self.background_color = "black" }
#define possible star colours
$colours = [white, grey, blue, pink, yellow]
#define the create star function
def draw_star(x, y)
$sky.pixel_color(x, y, $colours[rand($colours.length)])
end
#define starting coordinates
x = 0
y = 0
#define starting block
block = 0
while block < 100
#define random coordinates for the star in the block
rand_x = x + rand(20)
rand_y = y + rand(20)
#draw the star
draw_star(rand_x, rand_y)
#move the x coodinate position to the next block
x += 20
#check if its the last block on the row
if block % 10 == 0
y += 20
x = 0
end
#next block
block += 1
end
#create the image
$sky.write("sky.jpg")
And here is the output:

Beast is a new light-weight forum built with Ruby on Rails.
The simple design is great, and let’s the user discussions take center stage unlike most forums.
Take a look at Beast’s thread view page compared to vbulletin’s.
Vbulletin’s thread view page is bloated with useless features, while Beast’s is clean and easy to read. I wish more forums were designed this way, like the good old days of Yabb and Ikonboard.