richardoneill.com.au » Tags » Ruby

Showing all articles tagged with Ruby

Regular Expressions in Ruby and PHP

14 June 2007 PHP, Ruby, Programming

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 "$link\n";
}

?>

2 comments

Ruby Task

30 May 2007 Ruby, Programming

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

10 May 2007 Ruby

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.

3 comments

Working with mod_ruby

11 April 2007 Ruby, Programming

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

04 April 2007 Ruby, Programming

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.

2 comments

Canberra Web Design