Pages

Wednesday, November 2, 2011

How to install Ruby on Rails on mac

Update: There is an updated version of this article at How to Install Ruby on Rails on Mac - Updated

This is first tutorial in ruby on rails series. This post will show you the exact steps you need to follow to get Ruby on Rails up and running on your Mac.

If you are new to Terminal and Mac os X in general; you will find this helpful.

Since mac gives us shell access, we can take advantage of Linux goodies. The most useful and recommended tool is rvm. It stands for Ruby Version Manager. It's a command-line tool which allows you to easily install, manage, and work with multiple ruby environments including sets of gems. With new versions of ruby getting released periodically, it's nice to have a version manager.

OS X comes with ruby installation by default. Check the version of installed ruby -
 ruby -v
Most likely, you will see ruby 1.8.7 pre-installed for you. Now it's time to upgrade to either 1.9.2 or 1.9.3.

Step 1 - git installation
rvm requires git to be installed on your system. If you already have git, skip to the next step.
git is a popular version control manager used by many open source projects including linux kernel and android. Download the appropriate installer from here
http://code.google.com/p/git-osx-installer/downloads/list?can=3
Open the .dmg file and you will find the installer package. Follow the instructions to install git.
To verify, inside Terminal type git and you should see a list of available git commands.

Step 2 - RVM installation
For GUI lovers, there is GUI version available just for Mac users. Download from here - http://jewelrybox.unfiniti.com/
For rest of of us who love Terminal, follow along. It's one step process. Inside Terminal run this command -
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
It downloads rvm and installs it for you.

Step 3 - Load RVM
This step is analogous to adding rvm to your $PATH or environment variable.
Again inside Terminal,
echo ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && source “$HOME/.rvm/scripts/rvm”‘ >> ~/.bash_profile
It add rvm scripts to your bash profile so that every time you open the terminal, rvm gets loaded in the Terminal.

Step 4 - Verify RVM installation
Restart Terminal. Use cmd + Q to close the application and open it again.
Now it's time to see how rvm looks. Type rvm in Terminal and you will be greeted by a long message.

Step 5 - Download and Install xcode
xcode covers most of the dependencies for new Ruby installation. It also includes gcc compiler and other important development tools. You can download Xcode from Apple’s App Store. Search for “xcode” and click the install button.

Step 6 - Upgrade to Ruby 1.9.2
We will use 1.9.2 for this tutorial as it's the most recent stable version.
Inside Terminal,
$ rvm list known
# MRI Rubies
1.8.6[-p420]
1.8.6-head
1.8.7[-p352]
1.8.7-head
1.9.1-p378
1.9.1[-p431]
1.9.1-head
1.9.2-p180
1.9.2[-p290]
1.9.2-head
ruby-head
...
This is the list of all components known and managed by rvm.

We will install ruby 1.9.2.
$ rvm install 1.9.2
Next, check the installed versions by rvm using
$ rvm list
Now start using 1.9.2 by this command
$ rvm use 1.9.2

Your current ruby version is 1.9.2 but you might want to make this as your default version.
$ rvm --default 1.9.2
Check the version in another Terminal window
$ ruby -v
$ which ruby
You are almost done!

Step 7 - Install gems
Ruby gems are managed by a command line tool called 'gem' and it's preinstalled for you by rvm. You can use it to install other gems such as rails or sinatra etc.
$ gem install rails
rvm chooses gems depending on the current version of ruby and you can switch between different version by using 'rvm use' command.

Step 8 - Create Your rails project (optional)
 cd ~
 mkdir rails-apps
 cd rails-apps
 rails new hello
 cd hello
 rails s
In your browser, open localhost:3000/. You have successfully installed Ruby on Rails on mac :)

Tuesday, November 1, 2011

Using Macbook Pro like a Pro

Getting a new macbook pro at work was very exciting. I instantly fell in love with the trackpad and mouse gestures and the overall look and feel of all applications, the dockbar, the cute little animated effects. As I started setting up my development environment, I started missing my Ubuntu and Windows comfy zone.

The first shock was no home and end keys, instead a key combination. I still can't get over it. No page down and page up. And how do you refresh a page? Stop decreasing the brightness, it would help it could reload the page instead. Coming from Windows land, mac keyboard didn't feel native to me. But I promised myself to learn how to use mac like a pro. I will share some of my newly acquired wisdom here :)  


Friday, October 7, 2011

Silicon Valley Code Camp is back :)

I am so excited, the Silicon Valley Code Camp is starting tomorrow at Foothill College, Los Altos :)

This year with over 9 different tracks, 213 sessions and 3035 registered attendees, it's ready to break yet another record! More information here - http://www.siliconvalley-codecamp.com It's not too late to register if you haven't done yet. You can follow us at @sv_code_camp or #svcc

I will be helping with the attendees's registration, stop by to say hi.

Oh and don't miss the special session on Sunday morning at 9:15 am. Lino Tadros, a historian of Silicon Valley history,will retrace the steps of Apple as a special tribute to Mr Steve Jobs!

Happy Coding weekend!

Rebooting ....

Signing in ... SUCCESS (Glad you remember your password!)
Checking last entry ............ long time no see!
Update blogger interface [Y/n] n (I hate the new look)
Promise more regular blog entries? [Y/y/yes] .. (hmm)
List new topics here, type end to finish
 - Ruby On Rails
 - Interview Questions
 - Android 
 - HTML 5 experiments
 - end
Sounds interesting!
Publish now? [Y/n/later] Y
Congratulations!
Reboot Complete!

Wednesday, November 24, 2010

Polymorphic Data Structures Using C macros

Have you seen kernel source files, especially header files or any device driver implementation? They all make a heavy use of C macros. I always wondered why would anyone use C macros so much. Macros are not debug-friendly; you cannot set a breakpoint inside a multiline macros. I used to be afraid of macros until i realized during my kernel project that how benign macros could be.
For the record, C macros can be used for
  • defining "constants"
  • true inline functions with no type safety
  • enhancing readability of source code
  • writing platform independent code with help of conditional compilation
But there is more to it that can take advantage of the fact that C preprocessor runs before the compiler.
One of the more frustrating aspects of non-Object-Oriented languages like C is the apparent lack of re-usable code. For example, a primitive data manipulation structure (like a tree, queue, or list) that has been implemented in one structure cannot manipulate another structure without some workarounds. There are mainly two frequently used workarounds -
  • Copy &Paste - This means we copy the algorithm for one structure and by changing name create another data manipulation structure. Though it sounds time efficient method, maintaining different copies of same algorithm is pain and moreover if there exists any bug in algorithm, it's propagated to all copies.
  • Generalized data structures - These structures which accept only void * for data are frequently used but they come with a price too. The use of void * weakens the limited type safety of C language and allows for bugs to creep into the code. It also suffers from ppor performance because the system has to do twice as many memory references for both data and structural links.
None of the above solutions are as promising  as true object-oriented programming. But there is a way to achieve both efficiency and time safety by using making use of macros! By leveraging C preprocessor's text manipulation one can construct the C equivalent of a polymorphic data structure. Using this technique we can construct a reliable toolbox of primitive data structures that can be used in any C program, since they take advantage of C syntax and not the specifics of any particular implementation.

C preprocessor only does text substitution and does not care of the meaning of the strings. Let's take an example.
#define QUEUE(Q_TYPE, DATA_TYPE) \
struct Q_TYPE{ \
struct Q_TYPE *left; \
struct Q_TYPE*right; \
DATA_TYPE data; \
}
QUEUE(queue_t, int)  myQueue;
The above code generates a unique structure where only allowed data type is int. Hence, we get full advantage of C type safety and we can create structures for different data types on the fly. Sounds familiar? C++ templates? Then, we are on the same page.

The main power comes from the ability to generate algorithmic code which can work on these polymorphic data structure again using macros. Let's see some code -
I define a macro Q_NEW_HEAD(Q_HEAD_TYPE, Q_ELEM_TYPE) which generates a new structure of type Q_HEAD_TYPE representing the head of a queue of elements of type Q_ELEM_TYPE.

#define Q_NEW_HEAD(Q_HEAD_TYPE, Q_ELEM_TYPE) \
 typedef struct { \
  Q_ELEM_TYPE *front; \
  Q_ELEM_TYPE *tail; \
  int size;  \
 }Q_HEAD_TYPE
But I need a flexibility in choosing the link name for my structure. Hence, I define one more macro

#define Q_NEW_LINK(Q_ELEM_TYPE) struct Q_ELEM_TYPE*

This is how I can use these macros in my C program -

Q_NEW_HEAD(int_list_head_t, int_list_t);

typedef struct int_list_t{
  Q_NEW_LINK(int_list_t) myLink;
  int data;
} int_list_t;

Iterating over a linked list is the most common code, here is a syntactic sugar in the form of macros -

#define Q_FOREACH(CURRENT_ELEM,Q_HEAD,LINK_NAME)   \
 for (CURRENT_ELEM = (Q_HEAD)->front; CURRENT_ELEM; CURRENT_ELEM = CURRENT_ELEM->LINK_NAME)

Similarly we can define macros for
#define Q_INSERT_TAIL(Q_HEAD, Q_ELEM, LINK_NAME)
#define Q_INSERT_FRONT(Q_HEAD, Q_ELEM, LINK_NAME)
#define Q_INSERT_AFTER(Q_HEAD,Q_ELEM_TYPE,Q_INQ,Q_TOINSERT,LINK_NAME)
#define Q_REMOVE(Q_HEAD,Q_ELEM,LINK_NAME)
and so on ...
The program becomes much more readable and manageable. 

But, make sure to test all macro definitions for all corner cases, It's tricky to write and debug such long complicated macros. Here are some common pitfalls - well documented. Looking at the generated code may help sometimes - 

$ gcc -E my_macro_loaded_source.c