0

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.

sawa
  • 165,429
  • 45
  • 277
  • 381

3 Answers3

0

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

: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 }

dx7
  • 804
  • 5
  • 11
-1

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.

wohlert
  • 330
  • 2
  • 9