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.

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 »
Jun 032010
 

Internet-GlobeRecently, I faced a situation whiling moving fairly large image sharing website from shared hosting to a dedicated as it was taking too much resources. there were few challenges in this site transfer:

  • Number of images were over 90,000 and size of about 95GB which rules of conventional method of first downloading everything from old server  on your own machine and then uploading to new server is not feasible.
  • Shared hosting provider only gave FTP access. so it rules out rsync,scp,bittorrent or anything similar based transfers. host even refused to achieve the 90,000 files.

In short, you will have to move all files and database via FTP.  Here is how: Continue reading »

Apr 082010
 

Ever wanted to use Facebook chat without logging into Facebook website? Looking are a Desktop application for Facebook chat? Do you hate Facebook chat because it makes your browser go crazy ? If answer is “Yes” then Read On…

Recently, Facebook has enabled support for XMPP/Jabber protocol which means that now you can using standalone Instant Messengers like Pidgin(Works on Windows,Mac & Linux),iChat(Mac only).  This works on Windows,Linux and Mac on any application that support jabber protocol.

here I would demonstrate to do this on Windows with pidgin. Continue reading »

Feb 092009
 

Ever wondered why Hard drive model carry 10-14digit string, why not simple names. because model number of a hard drive can say lot of information about it e.g storage capacity,cache size & connector interface.storage companies use a set of conventions

Understanding a model no. would help you know about features of it by simply checking it label & you would no longer have to be dependent on not-so-informed salesman in your local shop. many times there are different model no. of exact same capacity but they differ in cache size which has a direct result on performance. more cache means faster performance

Now we will try to understand model number of various hard drives from different manufacturers.

Note: i would discuss only about consumer series internal hard drives from popular manufacturers. discussing others are beyond the scope of this guide.

Where do i find model no?
Its written clearly on faceplate of hard drive with bold characters along the serial no and other information like date of manufacturing etc.


1)Seagate

sample model number:ST3500630AS
we break is as: ST-3-500-6-30-AS

  • ST: Company initials. ST= “Seagate Technology”, STM – Seagate Technology Maxtor(Maxtor Hard drive).
  • 3: Form factor. 3 = Standard 3.5″ inch drives; 9 = 2.5″ inch Laptop drive.
  • 500: Total Capacity in Gigabytes(1000mb = 1GB). this is a 500GB model.
  • 6:Cache Size. 0 = 2MB, 2 = 2MB, 3 = 8MB,4 = 16MB, 6 = 16MB and 8 = 8MB. Note:the cache is the buffer memory between the controller and the mechanical hdd. the more cache it has – the more data it can store to transmit off to the controller. the faster data access on the hdd will be.please don’t confuse it with CPU Cache.
  • AS: Connector Interface type, A= 40pin PATA or IDE and AS=SATA interface.

Link:How to interpret Seagate model numbers

2) Western Digital

sample model number::WD6400AAKS
we break it as: WD-640-0-A-A-K-S

WD:Company initials

640: drive capacity in Gigabytes. its a 640GB drive

A:Form factor.

A:Brand/Series

K:RPM/Buffer Size or Attribute
S:Interface Type

Link: Model Number Format for WD Products