What is the difference between :limit
and limit:
? I am assuming the first is the method for limiting entry size in Ruby on Rails. What is the second for? I also don't know what is the significance of putting column n
on the right side.
3 Answers
This is probably an issue with hash notation.
Ruby 1.8 and prior use a style like this:
method(:limit => 10)
Ruby 1.9 and more recent have a new style that looks like:
method(limit: 10)
The new notation is a lot more like that in other languages like Python and JavaScript. They are functionally identical, though, as you can check with irb
where it always shows in conventional notation:
{ test: 'value' }
# => { :test => 'value' }
As to your question as to what limit
means, it really depends on what method you're passing this in to. In the context of a schema definition it can limit the size of a field:
t.string limit: 1024
If it's in the context of a query it might limit the number of results returned. Each method has their own interpretation so you'll need to consult the documentation on each method you encounter.

- 208,517
- 23
- 234
- 262
:limit
is a value of type symbol. You can see more about symbols on Ruby docs. http://ruby-doc.org/core-2.2.2/Symbol.html
limit:
is a syntactic sugar and can be used only as a hash key when this key is a symbol. Example: { :limit => 10 }
is the traditional way. After Ruby 1.9.3 you can rewrite that like { limit: 10 }

- 804
- 5
- 11
-
thanks , i thought it was not the same . – Apr 29 '15 at 18:27
Variables that have a colon :
before their name denounces that they are symbols (unique identifier) and this means that it is possible to do the following:
symbol = :limit
using the colon after the name is usually meant to indicate a hash key, such as the following:
hash = { limit: 5 }
puts hash[:limit] # returns 5
The confusion often ensues when working with older versions of ruby, where hashes are written as follows:
hash = { :limit => 5 }
puts hash[:limit] # returns 5
Which has the same exact meaning as the above statement.

- 330
- 2
- 9
-
-
`:limit = 5` => `SyntaxError: (irb):21: syntax error, unexpected '=', expecting end-of-input` – dx7 Apr 29 '15 at 18:19
-
-
`hash = { limit: 5 }; hash.limit` => `NoMethodError: undefined method `limit' for {:limit=>5}:Hash` – dx7 Apr 29 '15 at 18:21
-
`hash = { :limit => 5 }; puts hash.limit` => `NoMethodError: undefined method `limit' for {:limit=>5}:Hash` – dx7 Apr 29 '15 at 18:23