Apr 092013
 

The Ruby Programming language has two different ways by which you can represent strings in your programs:

  1. String
  2. Symbol
IMO, This stuff is the most confusing to people who are new to ruby & a friend of mine recently asked me to explain it(hence this blog post). so I am this post we will talk about the following:
  1. what is a symbol?
  2. The main differences between symbol & string?
  3. When to use symbol instead of string?
so, without wasting further time, here is the brain-dump of my explanation on symbols vs strings in ruby

What is a symbol?

A symbol is a string which is prefixed colon at starting. In other words, if you take ruby string such as “gaurish”, ‘gaurish sharma’ or “gaurish-sharma” & prefix a colon(‘:’) at te start, you get a symbol.  here is can example.

Two ways to do the essentially the same thing. Why?

Yes, you guessed it right. Strings & Symbols are essentially the thing. so why have two diffident “types” to represent string? The reason is how strings are implemented in ruby.

In Ruby, Everything is a Object including Strings

Due to SmallTalk influence, Ruby is fully object oriented language & there are no primitive types. Everything is implemented as Objects. so, when ever you try to assigned a string value to an variable like this x = “foo”, you are creating a new object in memory. Here is an small program which allocates same string to a variable 1,000 times & prints object_id of each string

As you may notice from terminal output of this program/script that each string has different object_id. ruby does creates a new object in memory every single time even thought the string is the same.

Wonder, if there is a another way?

How can I have strings which create object in memory once & all later operations refer to that previously created object with same object_id?

Glad: you asked. Enter Symbols! here is the same program with symbols.

In contrast with string, Notice how object_id is same for symbols which means its the same symbol.

What are the differences between Symbols & Strings?

  1. Symbols are immutable: Their value remains constant.
  2. Multiple uses of the same symbol have the same object ID and are the same object compared to string which will be a different object with unique object ID, everytime.
  3. You can’t call any of the String methods like #upcase, #split on Symbols.

When to use symbol instead of string?

Symbols generally have performance benefits. so will see their usage almost everywhere including getters & setters in classes, hash keys etc. here is few examples

#you will NEVER see this
class Foo
  attr_reader "name"
end

# Instead, you will ALWAYS see a symbol
class Foo
  attr_reader :name
end
#same thing but symbols are faster than strings

to use strings when:

  1. you want to use any of string methods like #upcase, #split, #downcase etc.
  2. you want to change/mutate the string

to use Symbols when:

    1. symbols can’t be changed at runtime. If you need something that absolutely, positively must remain constant
    2. Places where same string is going to be repeatably used, example hash keys are pretty good candidate for symbols. so instead of string keys, hash["key"] = value. you should  use symbols like this  hash[:key] = value 

To conclude, strings & symbols in ruby are similar but differences given above. But the main difference is that symbols are immutable & slightly more performant than strings so you should you them place where you know same string is likely to be repeated again & again.

Dec 162012
 

Recently, I have started contributing to Ruby on Rails — the popular web framework for quickly building web applications. I have just started but couple of my commits have been merged into rails core & have a pretty good idea on contributing  to rails. this guide details how you can start contributing to rails.

Step 0 – Decide what to contribute

It could be a bug or feature that you want aka starch your itch. It could a bug fix in which cause check github issue tracker for existing issues & see if you can fix anyone of them and send a Pull Request. It could be removing a warning from test. Or it would contributing documentation.

Step 1 – Fork & Change

This is rather simple but you must know how to use git – the popular distributed version control system. If you don’t then please following this guide closely & read multiple times until you understand.

  • Goto rails page on github & click the “Fork” button on the top. requires github account. so register if not already done. Its free!
  • You’ve successfully forked rails, but so far it only exists on GitHub. To be able to work on the project, you will need to clone it to your local machine.
    git clone https://github.com/your_github_username_goes_here/rails.git
    # Clones your fork of the repo into the current directory in terminal
    
  • Create a new branch
    $ git checkout -b my_branch
    
  • Create new rails app based on your fork
            $ /replace/with/path/to/your/rails/fork/railties/bin/rails new app_name_goes_here --dev
            
  • Do your changes. Now whatever changes you do, would instantly get reflected into your rails app. Once you are okay with said changed, commit them
                $ git commit -a -m "Describe your change in this message"
                # commit your changes. make sure to follow the guidance of a good commit message
           

Step 2 – Create a Pull Request

so you have made your change, tested it.Sweet! Now, its time to send your awesome changes to rails core team for review.

  • Add Upstream:
     $ git add remote git@github.com:rails/rails.git 
  • switch back to master, pull latest changes
             $ git checkout master
    	 $ git pull --rebase upstream master
    
  • Switch back your branch & Reapply your changes ontop of latest changes of the rails’ upstream master
     $ git checkout my_branch
    $ git rebase master
    
  • In the process of the rebase, it may discover conflicts. In that case it will stop and allow you to fix the conflicts. After fixing conflicts, use git add . to update the index with those contents, and then just run:
    $ git rebase --continue
    
  • Push branch to GitHub
    $ git push origin my_branch --force
    
  • Open Pull request using github’s web-interface.

Congratulation on your contribution! Now, wait for comments on the Pull request.

Step – 3

Once your Pull request is opened, most probably someone will suggest 1 or 2 changes.  hers is how to do them:

  1. switch to your feature branch
    	$ git checkout my_branch
    
  2. make changes & commit
  3. squash changes into one commit. rule is one feature == one commit
  4. force push

To close, let me welcome you the world of open source.  where code you write gets used by thousands of developers & affects lives of millions of people around the world. Its pretty amazing!

Sep 012012
 

Recently, I asked this question to group of people & they have given different answers:

Cloud computing is platform which provides  free space where we can keep our data securely.example gmail,Dropbox

Cloud  provides you software on rent

Cloud is the new type of hosting. Earlier we had shared,dedicated & now cloud hosting

Cloud allows you to rent computer(resources), so you can do work that requires i7 CPU with much slower intel Atom processor.

Are these people correct? this is what cloud computing means? Well…yes & no. Confused?

What is Continue reading »

Aug 122012
 
Note: This Search Engine Optimization Tutorial that I wrote years ago when i was into SEO but never published it. It had been lying as draft all these years, never finished. today, I discovered it while cleaning my drafts & decide to publish it in its current state as it is. so warning this might contains lot of errors, please read at your own risk.

this is basic tutorial which makes you understand the basics of Search Engine Optimization (SEO), search engine optimization is more than page optimization, it depends on many factors like link building, quality of incoming links etc.
So lets start with the basics.
What is search engine optimization?

Its a magic which no one can understand, kidding  Actually, It is a simple concept which any kid can understand. It works like this:

For a human being, to become popular he needs to do something extra ordinary so that whole world can know about it and he becomes known face in the world from nowhere. So same logic applies for websites also, to make your website popular first you need to do something good so that other people know about it, for example: you have a genuine site with good content and once people know about it they talk about the site (provide links, give your site links in forums, sites, blogs etc)

Search engine optimization is a process of taking something extra ordinary out of your site and let the whole world know about it.

Now to make your site known to Continue reading »

Aug 012012
 

AppFog Paas, a cloud hosting platform launched into general availability & released their pricing. Since, the pricing looks attractive, I decided to try Appfog over the weekend by migrating this wordpress blog over there. So, if you want to know how Appfog is or just want to learn how to host your wordpress blog on appfog for FREE, read on!

What is AppFog?

AppFog is a Cloud Hosting platform which is categorized as Platform as a Service(PaaS). PaaS is the thing that lets developers stay developers but still empower them to deploy applications that can reach millions.  Basically, PaaS saves you from having to do system administration & focus only on development of your web application. think of all the tasks you have to do when a new box is commissioned into production; they are silly things like installing web server,download & compile PHP/ruby, installing security update, firewall & security configuration & countless other small configurations & optimizations. With PaaS like Appfog,  you get that application environment ready out-of-the-box  & with lots of other features like scaling, cloning etc. Appfog supports multiple runtimes,  PHP(v5.3),Ruby MRI(v1.9), Java(v7), Node.js(v0.4 & v0.6), Python(v2.7) & Erlang(R14).

Why should I care about AppFog?

Agreed, there are a lot of other PaaS solutions like Google App Engine, Redhat’s OpenShift, Salesforce’s Heroku, Microsoft’s Azure and many others but I am going to give 2 things that according to me are special about Appfog:

  1. Ability to Choose Underlying Cloud Platform & Locations: Appfog lets you pick almost any cloud(current supported are AWS, Rackspace, Azure & HP’s Openstack) with choice of location where your app would be hosted(current locations are in US,Europe & Asia). This is in sharp contract with PaaS solutions like heroku where you are forced to host in US even half of your traffic comes from other parts of the world resulting increased latency. With Appfog, you can seamlessly deploy your application to multiple clouds at different locations. Thereby reducing SPOF
  2. Generous Free Quota: Free plan gives you total 2 GB RAM(per account) which scalable over multiple instances, 1GB Database(per account), 50 GB data transfer limit(per account) & 100mb diskspace(per application). Except the 100mb diskspace, all others are pretty generous.  There is a comparison of appfog with other PaaS solutions on what you have to pay for 4 instances with 512MB each:
    Pass Provider Price/mo
    dotcloud $276
    Heroku $107
    AppFog $0
Based, on the two reasons given Continue reading »