From the category archives:

Ruby

Ruby

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";
}

?>

{ 0 comments }

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.

Why's (Poignant) guide to Ruby.

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.

{ 0 comments }

Ruby Task

May 30, 2007

Ruby Star Field

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:

Ruby Sky

{ 0 comments }

Beast Forum

May 10, 2007

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.

Thread view

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.

{ 0 comments }

Working with mod_ruby

April 11, 2007

Ruby

While working with mod_ruby over the past week, I put together a small list of tips which might help other beginners like me…

403 Forbidden Error

After successfully installing mod_ruby, I would get a “403 Forbidden” error when attempting to run a ruby script. My apache log file showed..

[Wed Apr 11 10:32:13 2007] [error] access to /var/www/ruby/ruby.rb failed for (null), reason: Options ExecCGI is off in this directory

I simply added “Options +ExecCGI” to my httpd.conf file like so…

<Files *.rb>
Options +ExecCGI
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>

I was still getting a 403 error when running the script, but my apache logs were now reporting that the file permissions were incorrect. Easy fix, I chmod’d the file to 777, and the test script worked perfectly.

Incorrect HTTP Headers

I found that I could change the content type from text/plain to text/html by using this code…

r = Apache.request
r.content_type = 'text/html'
r.send_http_header
exit(Apache::OK) if r.header_only?There are other solutions to this problem.

Changes in included files not taking effect

mod_ruby caches scripts included using 'require'. Using 'load' instead will force mod_ruby to reload the included script.

Fetching GET variables

variable = Apache.request.paramtable['variable']

{ 0 comments }

Ruby First Impressions

April 4, 2007

Ruby

I’ve been trying to make an effort to learn Ruby for a few months now. Finally, the other night I decided I would start writing a small project in Ruby to see what everyone is talking about.

Fortunately Ruby comes prepackaged with CentOS, but the ruby-mysql module was not installed. This was my first minor problem. Attempting to install the mysql module, resulted in Make errors. I finally located the mysql-ruby RPM in the CentOS testing repo. Problem solved.

I admit, I underestimated Ruby. It’s more challenging than I expected, but it definitely brought the “fun” back into programming.

I’m still not sure about the best way to structure a Ruby application. I’ve had a look around some other open source apps though, and it’s slowly starting to make more sense to me. If anyone could point me in the direction of a good tutorial that would be much appreciated.

I’m having fun learning though, and I’m sure all my questions will be answered soon.

{ 0 comments }