Archive for May, 2007

What Version of Ruby Am I Using?

So you go to a different machine, and you spot Ruby on it (hallelujah), and you really have nothing to so, so out of curiosity you are wondering: What version of Ruby is it?

Go ahead and execute:

ruby -v

And you should get:

ruby 1.8.5 (2006-08-25) [i486-linux]

Within an Application

But what if you want access to that information inside a Ruby application?

Ruby comes with a set of constants which you can exploit to find out which version you have installed.

Testing for Version Compatibility

But what if the software only runs on certain versions of of Ruby, how do I throw an error if that criteria isn’t met?

For example Rails v1.2.3 only runs on Ruby v1.8.2 and above, except v1.8.3. In fact in v1.8.5 breakpoints are broken! (This is due to how breakpoints were initially implemented by exploiting a bug in Ruby, which got fixed in v1.8.5. Don’t worry though, in Rails 2.0 breakpoints are functional once again, without utilizing the Ruby bug.)

Well that makes for a (somewhat) complicated setup don’t you think?

The simple way is to split the RUBY_VERSION.

Giving you the major, minor, and tiny release numbers. Allowing you to do some version checking through conditions.

The Rails Way

Located at rails/lib/ruby_version_check.rb File

  • Line 3 does a simple regular expression check to ensure that its not running v1.8.3.
  • Line 10 just does a basic string comparison to ensure that the current version, is not less than the require minimum version.

Conclusion

Well thats enough to get you started, so get hacking!

Updated to reflect hindsight on my part

Comments (3)

ActiveRecord Migrations Without Rails

Continuing exploring ActiveRecord without Rails, I thought I’d look into using Migrations without Rails.

Once again, why?

  • You already are using ActiveRecord without Rails
  • Versionable instances of how your database looked like
  • Database agnostic
  • You forget how to crack out SQL code (happens to me, maybe too much ORM)
  • You want migrations in non Ruby Projects
  • boredom

One thing I’d like to emphasize is that you don’t have to use Migrations for Ruby Projects. You can use them in non Ruby projects as well. Just use them to create a versioned of the database.

By using Migrations, your (Ruby and non Ruby) developers will appreciate that they can easily see what changes were made to the database.

Note:

  • I won’t go into getting ActiveRecord working without Rails, please refer to that post. It’s dead simple.
  • All these commands are executed from the root directory of your non-rails application.

Creating the Migrations Directory

First we need a directory where the migrations will be stored, in this case I’ll use the name ‘db/migrate File

mkdir -p db/migrate

Now we’ll create our first migration, and save it as db/migrate/001_create_users.rb File:

Wonderful

Warning:

  • The numbers prefixed to the migration file’s name (in this case 001) is very important. DO NOT OMMIT THIS.
  • Notice the class name is CreateUsers, this should correspond to a camel case representation of the filename.

Setting up the Rakefile

Now we’ll need a Rakefile. If your not familiar with Rakefiles just create a file called: Rakefile File

Note: The :environment task sets up the ActiveRecord configurations, configure as appropriate/desired

Your First Migration

Executing the migration is dead easy:

rake

If you chose to log the output you’d get something like this:

  SQL (0.004480)   CREATE TABLE schema_info (version integer)
  SQL (0.001514)   INSERT INTO schema_info (version) VALUES(0)
  SQL (0.000000)   SQLite::Exceptions::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
  SQL (0.000246)   SELECT version FROM schema_info
Migrating to CreateUsers (1)
== CreateUsers: migrating =====================================================
-- create_table(:users)
  SQL (0.002094)   CREATE TABLE users ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) NOT NULL)
   -> 0.0026s
== CreateUsers: migrated (0.0028s) ============================================

  SQL (0.001499)   UPDATE schema_info SET version = 1

Perfect

Note: I am using SQLite in this example, your results may come out differently.

Your Second Migration

Knowing that our first migration works, lets create a second migration and test it out!

Now let’s create another migration: db/migrate/002_create_roles.rb File

And when executing rake Terminal, you should see the new table roles being created!

Wizard!

Moving Between Versions

At this stage we are at at version 2 of the database. But how do we go down to say version 0, before everything existed?

rake VERSION=0

What? You only want to go to version 1?

rake VERSION=1

Okay, enough showing off

Conclusion

That’s it, your ready to use and enjoy ActiveRecord’s Migrations Without Rails!

The Migration’s have a ton of features which you can and should utilize, which including adding indexes, controlling verbosity, renaming tables, dropping tables, and modifying existing tables (adding, renaming, modifying, deleting columns).

Just a whole alot of features, do check out the API for it!

Comments (9)

Will Ruby Not be the Next Big Programming language”?

After being announced the Programming Language of 2006 By the TIOBE Programming Community Index, it seems that Ruby might have met its match. Eventhough Ruby book sales seem to be growing at an alarming rate.

TIOBE Community Programming Index

Although, Ruby is the rising star if you look back 1 year, it is not growing anymore the last couple of months. Even worse, since April it is slightly declining. If this appears to be the new trend, then also Ruby does not become the “next big programming language”.

Java, C and C++ have been dominating the TIOBE index from the start and it seems as if they will keep this status for a long while. Possible new candidates are Lua (from 55 to 23 in 1 years time) and Groovy (from 103 to 52). These are, just like Ruby, lightweight scripting languages. I have the impression there is a maximum to such dynamically interpreted languages. Although very popular for web-based programming, statically compiled languages remain the core of all enterprise software systems. Based on this, I predict that the only candidate to change the top 3 is C#. This will not be achieved via hypes and vast jumps in the index but very gradually.

Note: Emphasis added by me

It’s still a bit premature Ruby, just hold on, we’ll still have another 6 months to go.

Don’t worry Ruby, I still love you for the school girl you are. Maybe you just need some time to grow up.

Worse comes to worse, the Malaysia Ruby Brigade will protect you, right guys?

Comments

« Previous entries