Ruby Category

Recently at work, we were having some problems with our application. Most of the problems stemmed from the complicated nature of the application and some poor design that we had been trying to patch up for months. Finally, in November, we got clearance from my boss to rebuild the application as a series of mini-applications that would run behind the main UI.

Initially, we planned on using nanite to distribute the application, passing messages between each mini-application, however, there was a problem getting the nanite mapper to work, so we explored other options and within a few hours we had a working version of beanstalk—a simple message queuing system. Here’s a quick tutorial on setting up beanstalk and using it in your Ruby or Rails application.
Read More

  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark

A lot of new Ruby developers I’ve worked with have seen the symbol notation—starting with a :— and have been confused by what a symbol actually is. There is a lot of information out there that is confusing as well. Here’s a brief rundown of what symbols are and how they are used.

Read More

  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark

I’ve been working on NORM and after a few proof of concept things, I wrote a test to test the create method for the base class.

Here’s the create method:

1
2
3
4
5
6
def create(attributes)
  columns = attributes.keys.join(", ")
  values = attributes.collect {|k, v| "'#{v}'"}.join(", ")
  @@connection.execute("INSERT INTO base (#{columns}) VALUES (#{values});")
  new(attributes)
end

I’ve written dozens of unit tests in Ruby on Rails, but what I didn’t realize was that the same library you use to test in Rails is the standard test/unit library came with the Ruby installation on my Windows machine. So, I wrote my own test which I later revised. Read More

  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark

Ruby DBI

No Comments

I’m pretty sure that the Ruby DBI gem is deprecated, however, I’m writing NORM so that I can experience the pain of refactoring as well as proving out that Test Driven Design has a place in modern development. It also helps because previous to the project I’m currently working on for my job at SolutionStream I have not used TDD and I’m anxious to see its power in a project like this one.

I found I needed the MySQL adapter for my application—I’m only building for MySQL right now. That required the dbd-mysql gem in addition to the dbi gem. After installing the gems, I found I could require them and then use them to connect to the database. Here are some code snippets from NORM that show how I’m using the DBI library.

First require the dbi and mysql gems.

1
2
require 'dbd/mysql'
require 'dbi'

Then, in my object, I’m storing the connection in a class variable.

@@connection = DBI.connect("DBI:Mysql:norm:localhost", "root", "")

Finally, I’m using the connection to perform operations on the database.

1
2
3
4
5
6
def create(attributes)
  columns = attributes.keys.join(", ")
  values = attributes.collect {|k, v| "'#{v}'"}.join(", ")
  @@connection.execute("INSERT INTO base (#{columns}) VALUES (#{values});")
  new(attributes)
end
  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark

class << self

No Comments

While working on NORM, I found a nice trick where you can enclose your class variables and methods inside a class << self … end block. I was aware of the ability to create class methods by using the def ClassName.method_name definition, but where I’m creating a series of class methods in NORM, I found this notation to be more useful.

I’m sure there will be more ruby stuff yet to be learned.

  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark