Web Development

Playing with data structures in Ruby

by Ali. 0 Comments

Sorting

I’ve been trying to sort a mixed array in Ruby the shortest way. Each element of the array by itself is a mixed array of a number and and a hash:

a = [
  [0, {:a=>"31", :b=>"21"}],
  [1, {:a=>"32", :b=>"11"}],
  [1, {:a=>"25", :b=>"19"}],
  [0, {:a=>"12", :b=>"10"}]
]
 
#sort by first item of each row (number)
a.sort{|x,y| x[0]  y[0]}
 
#sort by the first item in the hash
a.sort{|x,y| x[1][:a]  y[1][:a]}

Note: I initially posted this as a question on Stackoverflow and got different but correct answers.

Finding common elements

To find common elements between a number of arrays, simply add them to a hash and use the & operator, which creates a new array from two existing arrays containing only elements that are common to both arrays, omitting duplication. (See Techtopia)

a1 = [[1, 2, 3], [1, 2, 2], [2, 2, 3], [3, 2, 1]]
a2 = [[1, 2, 1], [1, 2, 2], [2, 2, 4], [3, 2, 1], [1, 2, 3]]
a3 = [[1, 2, 3], [1, 2, 2], [2, 2, 3], [3, 2, 1], [2, 2, 2]]
hash = {i1: a1, i2: a2, i3: a3}
common = hash.values.inject{|x, y| x & y}

What I can do with Ruby in just one line fascinates me.

government,politics news,politics news,politics

Reloading Rails console without exiting

by Ali. 0 Comments

In my journey to hopefully master Ruby on Rails, I’m amazed to see that many authors (in blogs or in books) most of whom are pretty expert in the field, choose to exit the Rails console completely (using CTRL+D) when they need to restart the environment (to clear up the objects in the memory, for example).

Instead there is a convenient method:

reload!

that you can type inside Rails console and it will reload the environment for you. No need to wait extra second to exit and reenter the console.

P.S. There might be a wisdom in not choosing realod! which I might not be aware of. In that case please enlighten me in the comments.

government,politics news,politics news,politics

Running MySQL 5.5 and Rails 3 on Mac OS X

by Ali. 15 Comments

A recent opportunity made me dust off my development skills and I decided to use Ruby on Rails as the framework of choice.

I’m happy with Ruby 1.8.7 for now (comes with Mac OS X Snow Leopard), but I had to update Rails to version 3, which was a painless process. After installing MySQL 5.5.10 (dumping MAMP for now), I installed the ruby gem for MySQL:

$ sudo gem install mysql2

You may want to run this too, although the first one should do the trick:

$ sudo gem install mysql

A quick test got me this piece of error:

LoadError: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found – /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2.rb:7
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require’
from (irb):1

You can never have a perfect installation experience. Can you?

A thread on Stack Overflow guided me to this solution:

$ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.10-osx10.6-x86_64/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

Note: As it happened with me, you might have the same problem that MySQL failed to create a link (alias) in the /usr/bin folder. You can fix it with a quick ln command:

$ sudo ln /usr/local/mysql-5.5.10-osx10.6-x86_64/bin/mysql /usr/bin/mysql

PS: This solution is based on the latest versions of the libraries available as of March 2011. You might need to change the versions in the commands or paths according to your machine settings.

government,politics news,politics news,politics

Fixing the multiprocessing error while developing for AppEngine

by Ali. 0 Comments

If you are developing for Google AppEngine on Mac, and you have updated python to 2.6.x or higher, you might face this non-sense error over and over:

ImportError: No module named _multiprocessing

The main reason behind this error is that AppEngine (as of this writing) can’t get along with python versions higher than 2.5.x.

Fortunately it is quite easy to fix this issue, especially if you are using GoogleAppEngineLauncher:

government,politics news,politics news,politics

The secret of staying productive while coding

by Ali. 0 Comments

Most people use versioning systems like Subversion, Mercurial, and git to maintain their code while working on software and web projects. There are also hosted solutions like github and bitbucket providing developers with more features than a traditional version control environment.

But sometimes you just want to keep your code in a safe place and make sure you have reliable backup of the different versions, and that’s it! For smaller (or personal) projects especially, using versioning systems is an overkill. But what could be the solution?

If I want to summarize it quickly, the ideal solution will be a one that:

  • Is hosted in the cloud (online)
  • Is easy to setup
  • Works without you doing any administration work
  • Is cheap and preferably free
  • Keeps you productive
  • Keeps coding synchronized on multiple workstations
  • Allows you to maintaing other related material, like PDF documentations, next to your code.

I think I found how to answer all these questions with one answer, let’s see how.

Storage as a service

Dropbox is a cloud-based storage service that keeps your files safe and in sync between multiple computers. It is basically created for backup and file synchronization, but I use it for coding and maintaining other contents like my web projects, and the book/article drafts including the drafts of this very article.

You will get 2GB of online free space which is pretty enough for many purposes, but it is always possible to upgrade to bigger plans.

Getting started

First create a Dropbox account and install the application (Windows, Mac, Linux) on all the computers you are working with. It is good to take a tour before , to check out the features.

Dropbox creates a local folder on your computer that is synced to your online folder. A mirror in the cloud, literally.

The solution I am proposing would work the best if all your computers run the same operating system, because you should set an identical path for the local folder on all computers. For example if you have a Mac and a PC, the folder structure difference will cause 2 different Dropbox folders (e.g. /Users/user/Dropbox and C:\MyDocuments\Dropbox\).

In my case, I set it to /Users/ali/Dropbox/ on both my home Macbook Pro as well as on my office Mac Pro. When I work at home (on my MacBook Pro) I store my ongoing work and it syncs with the remote folder. When I’m at work it sync back with my workstation and I can continue the work with no interruption.

The reason behind having similar folder structures is that when you are programming, you often use absolute file and folder references in the development environment. So having all computers using the same folder structure will guarantee you won’t face any silly issues in the code testing procees.

Using Dropbox

Now that you installed Dropbox and setup the folders, move a working folder or file, e.g a web project or a document you are working on, to the Dropbox folder. It will automatically start to synchronize the contents of the local folder with the remote folder (in the cloud). As you update the files, Dropbox will synchronize it with the remote folder and the good news is that it keeps a copy of every single version of the files as backup.

For the free plan the versions older than 30 days will be deleted, but in the paid version they will stay forever! so you will have all the earlier versions of your files.

On the other hand, when you open the other computers (that are configured), the local folder of Dropbox will be automatically synced with the remote folder, updating all files that have been updated in other computers. Then you can continue working on your project from the line you have left on the other computer, without carrying USB sticks around.

That was easy.

Happy yet?

Dropbox offers an intuitive web access interface to all versions of your files, so you can restore or download any older version. You can also setup a shared folder between you and other Dropbox users (say, your colleague) to exchange data in a productive way.

Dropbox Menu (Mac)
Dropbox Menu (Mac)

I am happily using Dropbox for syncing my files and it didn’t let me down even once. It also helps me keep the older versions in case I messed the current version. Every save you perform on your files is stored in Dropbox, so it is as if you have a live history of your work bit by bit.

government,politics news,politics news,politics