0

I am working with ruby on rails, before starting rails,I learn some ruby and I know about:symbols as these are unique through out our application

but while learning rails, i encountered a wired symbols symbol: when working with rails models as I have created the rails model as

def up
    create_table :users do |t|
        t.string "email", :limit =>50, :null =>false
        t.column "password", :string, :limit =>30, :null => false
        #data types,binary, boolean, data, decima, float, integer,  text, time
        #these are the differentdata types
        #we can also have options
        #:default
        #:precision for decimal
        t.timestamps null: false
    end
  end

and I was so confused between the :null and null: define in the above code, I know :null is symbol and what null: is ?

froderik
  • 4,642
  • 3
  • 33
  • 43
kemumaki
  • 53
  • 1
  • 7

2 Answers2

3

Both of them are the syntax for defining key-value pairs in a Hash in Ruby.

# Older Hash syntax; before Ruby 1.9 e.g. { :key => value }
    :limit => 50
    :null => false 

And:

# Latest Hash syntax; Ruby 1.9 and higher versions e.g. { key: value }
    null: false 
    limit: 50

In both cases, null is a Symbol.

See this short article on Ruby 1.9 and the new hash syntax

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
1

symbol: true is exactly the same as :symbol => true. symbol: is just a new syntax in ruby 1.9 and higher.

In ruby 1.8.7 and lower versions, only :symbol => true is supported. While in ruby 1.9+, both of these are supported.

Lei Chen
  • 81
  • 6