Archive for October, 2008

Installing the MySQL gem on OS X

Wanna install the mysql gem for Ruby? You’d better do it since it’s already available, and you will need it anyway from Rails 2.2 since all database adapters are going to be extracted to 3rd party gems. Take a quick look at your development.log file:

cat development.log | grep -i "DEPRECATION WARNING"

You may see a lot of deprecation warnings related to the mysql database connector like these:

DEPRECATION WARNING: You’re using the Ruby-based MySQL library that ships with Rails. This library will be REMOVED FROM RAILS 2.2. Please switch to the offical mysql gem: `gem install mysql` See http://www.rubyonrails.org/deprecation for details. (called from mysql_connection at /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:81)

Now you’d better install the mysql gem. I know, it is not a too easy task, a lot of people seem to have problems with it. If you type sudo gem install mysql your Terminal may return with errors:

Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
	ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Yea, very disappointing I know, I know. But rather then start crying like a teenage girl you could check the error log and start thinking about what you are missing.

No, no, no, I am not talking about a million dollar penthouse or girlfriend in this case… You forgot to tell RubyGems where to look for the mysql header/source files. Without them it just cannot build a native extension on your machine.

Let’s provide some more information to the package manager so it will be able to build that beast on your machine:

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql-2.7
1 gem installed

Much better.

Quick tip: Easily remove .svn and .gitignore

I’ve reinstalled my system a few days ago and upgraded all ports on my Mac.

Set up the new SVN server, created new repositories to store and keep my applications under version control. I’ve restored my folder from Time Machine which included the old .svn folders. Of course I wanted to remove all those ones before I import them to my new repository, so after some “googling” I’ve figured out how to do this from Terminal in the most efficient way.

Rather than going to each folder and delete all the old .svn folders and all the junk which had been left from my previous install, I managed to remove all these with one single command:

sudo find ~/Sites/myapp/ -name ".svn" -depth -exec rm -rf {} \;

If you are hardcore enough, you can also delete .DS_Store and .gitignore files too:

sudo find ~/Sites/myapp/ -name ".svn" -o -name ".DS_Store" -o -name ".gitignore" -depth -exec rm -rf {} \;

Article which helped me finding this method: find at dmiessler.com

Now you have a clean folder structure so you can start importing your application to your SVN repository :)

Quick tip: Variable Naming Conventions

I am just in the middle of a huge code re-factoring. I am dealing with a huge amount of code which was previously made by someone else. Most of you probably know that working with someone else’s code is most of the time a pain in the ass. It gets even worse when it leaks in documentation.

So, for this really simple reason I decided to start writing quick-tips about coding which will hopefully help keeping your code clean and easy to understand. Here’s the first one.

Use sensible and self-explanatory variable names

Think about how much time you often spend on figuring out what a variable is used for or what it is supposed to be. There are some bad examples:

# a lot of code before...
 
$b = SomeObject::getByID(12);
$na = "Cutting Edge";
$nu = 12;
$c = array('Audi', 'BMW', 'Mercedes', 'Aston Martin');
 
# a lot of code after...

Could you explain what those variables are meant to be? You will probably need to find the answer for that some other part of your code. A better way is to indicate the types and use self-explanatory variable names:

# a lot of code before...
 
$oBox = SomeObject::getByID(12);
$sName = "Cutting Edge";
$iNumber = 12;
$aCars = array('Audi', 'BMW', 'Mercedes', 'Aston Martin');
 
# a lot of code after...

In this example, the first characters indicate the data types of each variable (”o” means that the variable in subject is an object, “s” stands for strings, “i” indicates integers and so on. Pretty straight-forward.)

You may ask why this is a big deal. I tell you:

  • Easier to understand,
  • You know what to expect from a variable,
  • Others will understand the structure of your code a lot faster,
  • Also, it will be even easier for you too to maintain if you need to get back to your own code after a while.

So guys, please remember: Keep your code clean and sensible.

ECLM theme ported to XCode

A couple of months ago I’ve published our TextMate theme file which turned out to be really popular. Since that I’ve started experimenting with XCode but I could not get used to any of its default color themes.

XCode using the ECLM Color theme

XCode using the ECLM Color theme

I started to port our original TextMate theme to XCode. This is a quick screenshot of it, download will be available soon.