Symbol is greatly used in Ruby programs. It is an object of Symbol class. It represents some strings inside the Ruby interpreter. When you think that the string you are going to create would probably going to be reused somewhere again, then you should consider using symbols. The benefit of Symbol is really performance. During a Ruby program’s execution, as long as the symbol contents are the same, they are actually the same object, so it will refer to the same object in memory. e.g.

user1 = {:name => "Shanison"}
user2 = {:name => "Lin"}

Above codes actually only creates 1 symbol object, 2 strings object and 2 hash objects. Imagine that you are creating a lot of hash, the :name symbol would save a lot of object creation. You can even query all the symbols in your program:

Symbol.all_symbols # return an array of symbols

Enough about the introduction to symbols. What I want to talk about is actually about Hash. When constructing a hash, you would use symbol as the keys quite often. Probably due to this reason, in Ruby 1.9 it introduced a new Syntax for Hash.

user1 = {name: "Shanison"}
user2 = {name:  "Lin"}

At first glance, this looks pretty much like syntax for defining javascript object. Looks great. However, take note that the colon must be right after the key without any space. So it is not exactly the same as javascript object syntax.

user1 = {name : "Shanison"} # This will cause syntax error

This shorten syntax sometimes looks short and sweet when you pass it as a parameters.

server = Server.new(
addr: "192.167.123.1",
user: 'id_'
)
# Compares to Below
server = Server.new(
:addr => "192.167.123.1",
:user => 'id_'
)

However, do note that this syntax only works for symbol keys. So if you want to use strings or numbers as the hash keys, you can’t write the syntax in this way. e.g. Below code would return you error:

user1 = {"name": "Shanison"}  # Give Syntax error
user2 = {1:  "Lin"} # Give Syntax error

You can even mix the syntax when your hash has both symbol and numbers as keys, although the combination looks funny.

user1 = {name: "Shanison", 1 => "one"}

However, when your value is also a symbol, this syntax looks really funny:

user1 = {name: :source}

Due to above reasons, I still prefer the old syntax. It is just an options and personal preference, so there is no right or wrong in which syntax you adopt.

I attended the Red Dot Ruby Conference this year at NUS University Cultural Centre last Friday and Saturday. It was a two days event and attracted approximately 200 programmers around the world.

Llya Grigorik from google gave a very interesting talk on how to build a faster web using various technics. If you are experiencing slow website problem, you may want to check out his slides at Building a Faster Web.

There are also a lot of interesting topics that were discussed during the conference e.g PUBSUB infrastructure, Client Slide Templating, Using Redis to improve site performance, CoffeeScript etc.

Red Dot Ruby Conference 2012

Red Dot Ruby Conference 2012

If you want to parse a string into Time object, normally you would do the following

Time.parse('2011-12-31 08:56')
Time.parse('12/31/2011 08:56')

However, this parse method require the string to be in this specific format. If you try to run the following code:

Time.parse('31/12/2011 08:56')

You will get ArgumentError: argument out of range error. To parse string into Time with custom format, you can do this

require 'date'
def string_to_time time_, format_
 time = Date._strptime(time_, format_)
 return Time.local(time[:year], time[:mon], time[:mday], time[:hour], time[:min], time[:sec], time[:sec_fraction], time[:zone])
end

With the above function, you can call the parsing of time in this way:

string_to_time('31/12/2011 08:56', '%d/%m/%Y %H:%M')

I tried to find the Radiant CMS gem in my mac, so that I can open it with textmate to check the source code today. The following are two ways to find out the path to ruby gems.

1. Use irb
open irb, and type

require 'rubygems'
Gem.path

2. Use gem env command

gem env

From the output you should be able to find the ruby gem path. See the two yellow lines below:

RubyGems Environment:

- RUBYGEMS VERSION: 1.3.7

- RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0]

- INSTALLATION DIRECTORY: /Users/xinshan/.rvm/gems/ruby-1.8.7-p334

- RUBY EXECUTABLE: /Users/xinshan/.rvm/rubies/ruby-1.8.7-p334/bin/ruby

- EXECUTABLE DIRECTORY: /Users/xinshan/.rvm/gems/ruby-1.8.7-p334/bin

- RUBYGEMS PLATFORMS:

- ruby

- x86-darwin-10

- GEM PATHS:

- /Users/xinshan/.rvm/gems/ruby-1.8.7-p334

- /Users/xinshan/.rvm/gems/ruby-1.8.7-p334@global

- GEM CONFIGURATION:

- :update_sources => true

- :verbose => true

- :benchmark => false

- :backtrace => false

- :bulk_threshold => 1000

- REMOTE SOURCES:

- http://rubygems.org/

 

There are times you want to check if a string is a valid json in ruby, here is what you can do:

  require 'rubygems'
  require 'json'

  def valid_json? json_
    JSON.parse(json_)
    return true
  rescue JSON::ParserError
    return false
  end

The above method will try to parse a string to json using Ruby’s Json parse, if it fails with exception, we know that it is invalid json. It looks simple, but you may not realize that you can do it this way.
Read the rest of this entry »