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.

Free Ruby on Rails Book

Posted by Ryan Baxter Thu, 25 Oct 2007 13:51:00 GMT

Free? Yes, that’s right! A totally free Ruby on Rails book. Those fine fellows at Rails Envy mentioned it in episode #2 of the Rails Envy Podcast. What’s the catch? You only have 37 days from today to download this book. Do it now! Thanks to Patrick Lenz for writing the book and thanks to the guys at Rails Envy for making it known.

rake aborted! No such file or directory - /tmp/mysql.sock

Posted by Ryan Baxter Sat, 06 Oct 2007 20:40:00 GMT

If you’re running into the above error message when trying to perform a Rails migration on Debian or Ubuntu then you might try adding the following line to your database.yml file:

socket: /var/run/mysqld/mysqld.sock

My database.yml file looks sorta like this:

development:
  database: crunchlife_development
  adapter: mysql
  host: localhost
  username: mysql_user
  password: @!#?@!
  socket: /var/run/mysqld/mysqld.sock 

test:
  database: crunchlife_test
  adapter: mysql
  host: mysql.ryanbaxter.net
  username: mysql_user
  password: @!#?@!
  socket: /var/run/mysqld/mysqld.sock

production:
  database: crunchlife_production
  adapter: mysql
  host: mysql.ryanbaxter.net
  username: mysql_user
  password: @!#?@!

Good luck and happy migrations!

Version Control Explained

Posted by Ryan Baxter Sat, 06 Oct 2007 18:44:00 GMT

Kalid, from BetterExplained.com, has posted an excellent explanation of the hows and whys behind version control. The article takes an agnostic approach to versioning, but its examples will be more beneficial if Subversion is used. Kalid’s explanation also contains the definitions of words commonly used in the versioning nomenclature. I like this. You may read Kalid’s article and think, “Yeah, tell me something I don’t already know.”, but it is written in terms that your PHB (Pointy-Haired Boss) might understand. Spread the good word.

@!#?@!

Posted by Ryan Baxter Sat, 06 Oct 2007 18:32:00 GMT

I’ve been layed up in the hospital looking sorta like Q*bert for a couple of days. More to follow.