Topic: beginner's question => vs = and nil vs null

Hi!

I have tried googling this with no success, so I turned to the forums for help

    1. in rails, what is the difference between => and = ?

    2. what is the difference between nil in rails and null in general programming? is there a reason nil is not called null in rails?

Thanks in advance!

Last edited by henk (2011-02-01 07:41:04)

Re: beginner's question => vs = and nil vs null

1. '=>' is used when assigning a value to a key in a Hash like that:

h1 = { "a" => 1, "c" => 2 }

'=' is used to assign a value (on the right) to a variable (on the left). The priciple is the same in all the programming languages.
2. Null is generally a value. In Ruby nil is an object, or more exactly "The class of the singleton object nil. ", see Ruby API for more details.

Last edited by Javix (2011-02-01 08:15:12)

Re: beginner's question => vs = and nil vs null

1/

=> is the same than php. It's "value to key" assignment operator

hash = { :key => 'value', :key2 => 'value2' }

puts hash[:key] # => value
puts hash[:key2] # => value2

= is the same than in (almost) all languages. It's a left assignment.

a = 5
puts a # => 5
puts a + 1 # => 6

2/

nil is ruby's null. Don't bother yourself with technical introspection into ruby internals (nil is a singleton instance of NilClass ...).

Re: beginner's question => vs = and nil vs null

thanks