Installing Ruby 1.8 and 1.9 on Ubuntu from Source

Posted by Ryan Baxter Sun, 14 Sep 2008 20:30:00 GMT

I’ve begun testing my fractal library with the latest source code from the Ruby 1.9 trunk. Since 1.9 is a development release and I still need 1.8 for my Rails applications, I’ve checked out both versions and configured them to run side-by-side on Ubuntu, Hardy Heron.

Before you begin, make sure you have the autoconf, build-essential, and subversion packages installed.

It may also be a good idea to include the Ruby1.8 and Ruby1.9 build dependencies from Ubuntu’s package repository. This could prevent some headaches later on.

$ sudo apt-get build-dep ruby1.8 ruby1.9

Create a directory for the Ruby source code.

$ mkdir /home/ryan/source
$ cd /home/ryan/source

Check out the code from the 1.8 branch. Since this branch includes patches, you can always update your source and recompile when new patches are released.

$ svn co http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8 ruby1.8

Next, create a configuration, configure, and compile.

$ cd ruby1.8
$ autoconf
$ ./configure --prefix=/opt/ruby1.8 --program-suffix=1.8
$ sudo make
$ sudo make install

One of the best parts of Ruby is Rubygems! Download and install it.

$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
$ tar -xvzf rubygems-1.2.0.tgz
$ cd rubygems-1.2.0
$ sudo ruby1.8 setup.rb

Finally, link your new binaries to the /usr/local/bin directory.

$ sudo ln -s /opt/ruby1.8/bin/* /usr/local/bin

Typing ruby1.8 -v in your console should yield something similar to the following:

$ ruby1.8 -v
ruby 1.8.7 (2008-09-15 revision 19348) [i686-linux]

The current source for Ruby 1.9 requires version 1.8 to compile, but if you’ve followed my directions up to this point you should be ready to download and compile the latest Ruby 1.9 source code.

Go back to your source directory and check out the latest code from the 1.9 trunk.

$ cd /home/ryan/source
$ svn co http://svn.ruby-lang.org/repos/ruby/trunk ruby1.9
$ cd ruby1.9

This time use the –with-baseruby switch when configuring. Set this option to the new Ruby 1.8 binary and then compile.

$ autoconf
$ ./configure --with-baseruby=/usr/local/bin/ruby1.8 --prefix=/opt/ruby1.9 --program-suffix=1.9
$ sudo make
$ sudo make install

Finish by linking your new Ruby 1.9 binaries to /usr/local/bin.

$ sudo ln -s /opt/ruby1.9/bin/* /usr/local/bin

Typing ruby1.9 -v in your console should yield something similar to the following:

$ ruby1.9 -v
ruby 1.9.0 (2008-09-15 revision 19351) [i686-linux]

Ruby 1.9 includes Rubygems! We’re done!

Since the binaries we’ve created have been suffixed with either 1.8 or 1.9, you must remember to execute them with their proper name ie (gem1.8, gem1.9, ruby1.8, ri1.9).

I’ve written these instructions from memory so if you have any problems, please post them and I’ll try to help out as best I can. Please be warned, however, that not all code written for Ruby 1.8 will work in 1.9.

Ruby Fractal Library

Posted by Ryan Baxter Thu, 03 Jul 2008 21:26:00 GMT

Last October, I wrote a small fractal rendering program in Ruby using the Shoes windowing toolkit written by why the lucky stiff. It’s sole purpose was to test Shoes. The code was painfully slow at rendering the Mandelbrot set, but it did, however, begin a small obsession of mine with fractals.

Since I couldn’t find a fractal library for Ruby, I decided to write one. Over the last two weeks I’ve written some code to generate both the Mandelbrot and Julia set fractals using the escape time algorithm. The code is still slow, but within a couple weeks I hope to replace the slow portions with inline C.

There may still be some bugs and I haven’t added any error handling, but here it is. An “almost” pure Ruby fractal library. Once this is cleaned up I’ll repost the code. I suppose a gem could be possible as well. Happy 4th!

fractals.rb

# RB

require 'rubygems'
require 'complex'
require 'png'

module Fractals
  module Fractal
    attr_accessor :begin_range, :end_range

    def initialize(begin_range, end_range)
      @begin_range, @end_range = begin_range, end_range
    end

    def draw(height=250, width=250, m=1.0, save_as='fractal.png')
        canvas = PNG::Canvas.new(height, width)

        # Find the complex coordinate for each pixel.
        0.upto(height - 1) { |y|
          i = (y * (@end_range.image - @begin_range.image) / height +
          @begin_range.image) * m
          0.upto(width - 1) { |x|
            r = (x * (@end_range.real - @begin_range.real) / width +
            @begin_range.real) * m
            if self.in_set?(Complex(r, i)) then
              canvas[x, y] = PNG::Color::Black
            else
              canvas[x, y] = fetch_color(self.last_iteration, self.max_iterations)
            end                    
          }
        }

        png = PNG.new(canvas)
        png.save(save_as)
    end  

    private
    def fetch_color(last_iteration, max_iterations)  
      divisor = 765*last_iteration/max_iterations
      case divisor
        when 0..254 then return PNG::Color.new(divisor%255, 0, 0, 255)
        when 255..509 then return PNG::Color.new(255, divisor%255, 0, 255)
        when 510..765 then return PNG::Color.new(255, 255, divisor%255, 255)
      end       
    end
  end

  class Julia
    include Fractal
    attr_accessor :seed, :bailout, :max_iterations
    attr_reader :last_iteration

    def initialize(seed=Complex(0.36, 0.1), bailout=2, max_iterations=100,
    begin_range=Complex(-2.25, -1.5), end_range=Complex(0.75, 1.5))
      super(begin_range, end_range)
      @seed, @bailout, @max_iterations = seed, bailout, max_iterations
    end

    def in_set?(z)
      @max_iterations.times { |i|
        z = z**2 + @seed
        if z > @bailout then
          @last_iteration = i
          return false
        end      
      }
      return true
    end
  end

  class Mandelbrot
    include Fractal  
    attr_accessor :bailout, :max_iterations
    attr_reader :last_iteration

    def initialize(bailout=5, max_iterations=100, begin_range=Complex(-2.25,
    -1.5), end_range=Complex(0.75, 1.5))
      super(begin_range, end_range)
      @bailout, @max_iterations = bailout, max_iterations
    end

    def in_set?(c)
      z = 0
      @max_iterations.times { |i|
        z = z**2 + c
        if z > @bailout then
          @last_iteration = i
          return false
        end            
      }
      return true
    end
  end
end

Using this library is as simple as the following:

require 'fractals'

mandelbrot = Fractals::Mandelbrot.new
mandelbrot.draw

Any suggestions/bug fixes can be posted here. Thanks.

Compiling the Gosu Game Development Library on Ubuntu Feisty

Posted by Ryan Baxter Wed, 21 Nov 2007 01:40:00 GMT

I’ve had the itch this week to try out a few of Ruby’s game development libraries. I decided to start with Gosu. Gosu is a 2D game development library for the Ruby and C++ programming languages. It’s available for the Mac OS X, Windows, and Linux platforms and integrates with both the RMagick image processing library and the Chipmunk physics library.

Unfortunately the library is only available as a gem for Mac OS X and Windows. As a Linux user I’ve become accustom to compiling my own libraries, but this often means downloading and compiling dependencies, swearing, and a lot of Googling. I was lucky. Compiling Gosu only caused a few hairs to fall out.

If you’re an Ubuntu Feisty user, you’ll need to add the following line to the LargeImageData.hpp file located in the gosu-source-0.7.7/Gosulmpl/Graphics directory:

#include <boost/none.hpp>

This line adds a reference needed on line 31 of the LargeImageData class. With this addition, you can then execute the commands below from the gosu-source-0.7.7/linux directory to compile the Gosu game development library.

$ autoconf
$ ./configure
$ make

The make file provided with the source does not add the compiled gosu.so file to the Ruby lib directory. I chose to do this manually rather than edit the make file. Just issue the following command from the gosu-source-0.7.7/linux directory to add your newly compiled library.

$ sudo cp gosu.so /usr/lib/ruby/1.8/i486-linux/gosu.so

With a little elbow grease I successfully compiled the Gosu game development library on Ubuntu Feisty. I’ll dig into some tutorials next and post back with my results.

Be sure to check out Gosu’s, Getting Started on Linux, for a list of dependencies and compilation instructions.

/usr/lib/ruby/1.8/rinda/ring.rb:212:in `lookup_ring_any': RingNotFound (RuntimeError)

Posted by Ryan Baxter Sun, 18 Nov 2007 23:44:00 GMT

If you’re experiencing the error message above while using the Rinda distributed computing module in your Ruby script then try defining a Domain name in your Network Settings configuration. This has worked in my experience using Rinda and Ruby on Ubuntu Feisty.

Code Snippet: Ruby Walks with Shoes

Posted by Ryan Baxter Tue, 30 Oct 2007 14:30:00 GMT

_Why of Why’s (Poignant) Guide to Ruby has announced the publication of a 52 page comic style book illustrating the ins and outs of his new windowing toolkit, Shoes. In his words, “Shoes is a very informal graphics and windowing toolkit. It’s for making regular old apps that run on Windows, Mac OS X and Linux. It’s a blend of my favorite things from the Web, some Ruby style, and a sprinkling of cross-platform widgets.”

Using the the latest Shoes revision, I’ve written some code to generate a Mandelbrot fractal. Try it out with the following command (assuming your script is located in the Shoes samples directory):

./shoes samples/mandelbrot.rb

You may want to go make a sandwich while you wait. Here is a sample of the output:

Here is the code:

# RB

require 'complex'

class Mandelbrot  
  def initialize(bailout=10, iterations=100)
    @bailout, @iterations = bailout, iterations   
  end  

  # A method for determining if a point is within
  # the Mandelbrot set.
  def in_set?(x, y)
    c = Complex(x, y)
    z = 0
    @iterations.times do |i|
      z = z**2 + c                       
      return false if z > @bailout
    end
    return true 
  end  
end

class Generator < Shoes
  url '/', :index

  def index()    
    render(250, 250, rgb(205, 102, 0))    
  end

  def render(height=250, width=250, color=rgb(255, 0, 0))
    nostroke
    fill color   

    mandelbrot = Mandelbrot.new

    # Render the fractal.
    0.upto(height) do |y|
      0.upto(width) do |x|
        scaled_x = -2 + (3 * x / width.to_f)
        scaled_y = 1 + (-2 * y / height.to_f)
        if mandelbrot.in_set?(scaled_x, scaled_y) then        
          rect :left => x, :top => y, :width => 1, :height => 1
          # oval :left => x, :top => y, :radius => 1, :center => true                           
        end      
      end
    end
  end
end

Shoes.app :title => 'Mandelbrot', :height => 250, :width => 250

The NetBeans Ruby IDE

Posted by Ryan Baxter Thu, 25 Oct 2007 14:26:00 GMT

Some time ago I wrote about my search for the perfect Ruby IDE. I understand that the criteria for perfect is different for everyone, but in my previous article I outlined the eleven features that best describe my perfect Ruby IDE. I will admit that I haven’t kept up with my analysis. Like everyone else, I have a lot of other things to do and evaluating a dozen IDEs takes a lot of time. Apologies aside – I have found an IDE that fits all of my expectations. My search might be over.

Roman Strobl’s recently published article, NetBeans: Ruby Developer’s New Best Friend, highlights the features and improvements of NetBeans’ efforts in the Ruby IDE arena. I’m consistently amazed by the quality of writing published by InfoQ and Strobl’s work is a fine example. I recommend reading his article and then downloading the NetBeans Ruby IDE to test drive it yourself.

NetBean’s Ruby IDE has the best code completion I’ve found in a Ruby IDE. It rivals Visual Studio on the .NET platform. The IDE is lightweight. At no point during my testing did it ever slow down. Its source control integration works well. The default font is easy on the eyes and compliments the Ruby syntax highlighting scheme. My favorite feature is the garbage collection button located in the upper right-hand corner of the application. Some developers might think the application poorly designed for needing this feature, but coming from Visual Studio, I applaud it.

The NetBean’s Ruby IDE is still in development, but the testing I’ve done hasn’t uncovered any issues worth mentioning. While working on one of my side projects I haven’t noticed any appreciable difference between the Windows and Linux versions. Have I mentioned yet that the NetBean’s Ruby IDE is free? I’ve found my new daily driver. Kudos NetBeans!

Download it here.

I Want My IDE Update

Posted by Ryan Baxter Tue, 21 Aug 2007 04:14:00 GMT

I started writing some Ruby code this past weekend for a new project that I’m involved in. Rather than download an IDE mentioned in my previous article, I Want My IDE, I decided to use the gedit text editor that comes bundled with Ubuntu Linux.

Attempting to capture the experience Mac users get from using TextMate, I installed the Class Browser, File Browser Pane, Project Manager, Snap open, and Snippets plugins from live.gnome.org. I also enabled line numbers, current line highlighting, and bracket matching. It’s not quite TextMate, but it’s not a bad choice either.

I had initially stated that my top two needs of an IDE were Subversion (SVN) integration and code completion. I didn’t have either of these with gedit, but I didn’t find myself missing them either. I’ve grown accustomed to using SVN from the command-line and ruby-doc.org didn’t slow me down too much while looking up class definitions.

Overall I’m quite pleased, but I have to admit that installing gedit plugins was a bit tricky and I never did get my fonts to look like those found in the TextMate-like Gedit tutorial.

Code Snippet: Ruby Image Scraper

Posted by Ryan Baxter Tue, 14 Aug 2007 03:46:00 GMT

I stumbled upon a screen scraping library for Ruby last week called scrAPI. It’s extremely flexible and can be seen in action on the co.mments blog post scraper. The scrAPI library can be installed by issuing the following command from your console:

gem install scrapi

Testing scrAPI was fairly easy once I figured out how to define a scraper. With that aside, I wrote a small script that saves images from a URL provided by the user. The scrAPI library could be used for good or evil, but only you can decide.

#!/usr/bin/ruby

require 'fileutils'
require 'open-uri'
require 'pathname'
require 'rubygems'
require 'scrapi'

# Get the URL input.
puts 'Enter a URL:'
url = gets.chomp

# Get the HTML source.
html = nil
open(url) {|source| html = source.read()}

# Define the scraper.
scraper = Scraper.define do
  array :images
  process "img", :images => "@src"
  result :images
end

# Scrape the HTML for images.
images = scraper.scrape(html)

# Create a directory to save the images in.
directory = url.gsub(/http:\/\//, '')
FileUtils.mkdir directory

images.each do |image_path|
  # Determine if image_path is absolute or relative. 
  path = Pathname.new(image_path)  
  if not path.relative? then image_path = url + image_path end

  # Write the image to disk.
  open(image_path) do |source|
    file_name = image_path.split('/').last
    open(directory + '/' + file_name, 'wb') {|file| file.write(source.read())}
  end
end

puts 'Finished...'

I Want My IDE

Posted by Ryan Baxter Fri, 10 Aug 2007 02:32:00 GMT

The majority of my academic and professional programming career has been spent writing code using an integrated development environment (IDE). I’ve dabbled with Eclipse, Microsoft Visual Studio, Macrodobe Dreamweaver, and various Borland products. Rather than juggle multiple text editors and source control consoles, I find it easier to stay organized using an IDE on large projects. When editing config files or writing scripts I prefer a lightweight text editor. In Linux, vi or gedit is my choice. Notepad2 is at the top of my Windows list.

A few months ago I decided to learn Ruby and the Ruby on Rails framework. I began with the obligatory Hello World program and quickly progressed through a series of tutorials using vi and gedit to get the job done. Since then, I’ve begun some larger projects and am quickly finding myself losing focus and missing the benefits of an IDE. Consulting Google, I compiled a list of prospective IDEs to begin my evaluation. I’m willing to give each of them a fair chance at becoming my Rails development environment, but have a few questions before I begin. What, if any, IDEs have I missed? How long should I try each one?

Needs (in order of importance):

  1. Linux compatible
  2. Project Browsing
  3. SVN integration
  4. Syntax Highlighting
  5. Code Completion
  6. Active Community
  7. Unit Testing
  8. Debugging
  9. Auto-indent
  10. Plugin support
  11. Less than $100

The list:
*Each IDE/editor was capable of Project Browsing, Syntax Highlighting, and compatible with Linux.

  1. Aptana RadRails
    Pros
    • Good SVN integration.
    • The latest Beta has working Code Completion.
    • Built on Eclipse.
    • More Rails features than Eclipse + DLTK.
    • Many plugins inherited from Eclipse
    • Free.
    Cons
    • Code Completion is broken in the current stable release.
    • Built on Eclipse.
  2. Eclipse + DLTK
    Pros
    • SVN integration.
    • DLTK has Code Completion.
    • Tried and true.
    • Vast library of plugins.
    • Active community.
    • Free.
    Cons
    • Eclipse is slow and consumes a lot of memory.
  3. FreeRIDE
    Pros
    • Auto-indenting.
    • Debugging.
    • Free.
    Cons
    • No SVN integration.
    • No Code Completion.
    • Performance could be an issue b/c it’s a native Ruby application.
  4. gedit + plugins
    Pros
    • Lightweight.
    • Plugins.
    • Free.
    Cons
    • No SVN integration.
    • No Code Completion.
  5. jEdit
    Pros
    • SVN integration.
    • Code Completion.
    • Plugins.
    • Free.
    Cons
    • Not user friendly.
  6. IntelliJ IDEA 6.0
    Pros
    • SVN integration.
    • Code Completion.
    • Debugging.
    • Unit Testing.
    • Plugins.
    • Much more…
    Cons
    • $249.
  7. Komodo IDE 4.1
    Pros
    • SVN integration.
    • Code Completion.
    • Debugging.
    • Built specifically for Ruby on Rails.
    • Much more…
    Cons
    • $295
  8. Mondrian Ruby IDE
    Pros
    • Lightweight.
    • Free.
    Cons
    • No SVN integration.
    • No Code Completion.
    • Performance could be an issue b/c it’s a native Ruby application.
    • Spam in support forum.
  9. NetBeans Beta 6.0 Milestone 10+
    Pros
    • SVN integration.
    • Code Completion.
    • Debugging.
    • Plugins.
    • Free.
    • Much more…
    Cons
    • Beta.
  10. Ruby IDE from CodeGear
    Pros
    • CodeGear experience.
    Cons
    • Feature set not yet released.

I’ll be evaluating each of the IDEs/editors in turn and publishing my results as a series. Feel free to leave feedback and check back soon!

Code Snippet: Ruby Word Masher

Posted by Ryan Baxter Sat, 28 Jul 2007 20:54:00 GMT

I can be extremely indecisive about things. So much, in fact, that I even wrote a script to help me choose a name for this website. Is it strange that a random number generator can make me feel better about making decisions? My wife thinks I’m crazy, but she also calls domain names, donames. Besides, I think there is something novel in a computer choosing a name for itself. Anyway… Given some user input, the code below will read words from a file and then mash them together to provide unique combinations. Here is a words.dat file to help get you started. Happy mashing.

#!/usr/bin/ruby

words = Array.new
i = 0

def mash_words(words, mash_count)
  new_word = ''

  1.upto(mash_count) do
    new_word += words[rand(words.length)]
  end

  return new_word
end

begin
  puts 'How many mashed words would you like to create?'
  word_count = gets.chomp.to_i  

  puts 'How many words would you like to mash?'
  mash_count = gets.chomp.to_i  

  input_file = File.new("words.dat", "r")
  while (line = input_file.gets)    
    words[i] = line.chomp    
    i += 1
  end  

  1.upto(word_count) do    
    puts mash_words(words, mash_count)
  end  

  input_file.close  
rescue => err
  puts "Exception: #{err}"
  err
end

Older posts: 1 2