28

What I understand so far is that :variable in ruby, is to say that this variable will not be able to change, which is similar to constant in other language. Am I correct??

Then my confusion is, sometimes I see the colon in front of variable, like this

Rails.application.config.session_store :cookie_store, 
      key: '_blog_session'
  <%= link_to "Delete", article, confirm: "Are you sure?", 
      method: :delete %>

Both key: and method: has colon in front.What does that this represent?

Furthermore

Blog::Application.routes.draw.do
  root :to => "articles#index"
end

There are double colons in between variables?

I am guessing that Blog: is one variable, and :Application is constant, which I doubt it is. Please enlighten me?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
vdj4y
  • 2,649
  • 2
  • 21
  • 31
  • This is neither a variable, nor a constant. It's literal syntax to Ruby [Symbol](http://www.ruby-doc.org/core-2.2.0/Symbol.html). `Blog::Application` syntax isn't related to symbols, it's about resolving [namespaces](http://ruby-doc.com/docs/ProgrammingRuby/html/tut_modules.html). – Marek Lipka Feb 05 '15 at 08:10
  • Just to be clear, do you mean Blog is a namespace? or Application is a namespace? what about colon in front of, like method: or key: – vdj4y Feb 05 '15 at 08:19
  • Blog is namespace. Look at the link I provided. Syntax like `{method: 'delete'}` etc. is a hash syntax, equivalent to `{:method => 'delete'}`. – Marek Lipka Feb 05 '15 at 08:21
  • I see. I will check your link, thanks. I should give you the points then. :) – vdj4y Feb 05 '15 at 08:24

3 Answers3

63

What i have understand so far is that :variable in ruby, is to say that this variable will not be able to change, which is similar to constant in other language.

I'm not sure if I understand that statement. In Ruby, constants start with an uppercase letter:

Foo = 1

Reassignment generates a warning:

Foo = 1
Foo = 2 #=> warning: already initialized constant Foo

Variables start with a lowercase letter and reassignment doesn't cause a warning (they are supposed to change):

foo = 1
foo = 2 # no warning

Symbols start with a colon:

:a_symbol
:Uppercase_symbol
:"i'm a symbol, too"

They often represent static values, e.g. :get and :post. Symbols are memory efficient, because they are created only once - the same symbol literal always returns the same object. Checking if two symbols are equal is a cheap operation.

Both key: and method: (...) What does that this represent?

This is an alternate syntax for hashes. You can type it in IRB to see the result:

{ foo: 1, bar: 2 }
#=> {:foo=>1, :bar=>2}

There are double colons inbetween variables? now I am guessing that Blog: is one variable, and :Application is constant.

No, Blog and Application are both constants and :: is the scope resolution operator. It can be used to access nested constants, e.g.:

module Foo
  class Bar
    BAZ = 123
  end
end

Foo::Bar::BAZ #=> 123
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • @Stefan one more confusion `params = { }` params[mehtods:] = :delete syntax error, unexpected '] ` but if params[:mehtods] = :delete now it is fine why ? – Gupta Feb 05 '15 at 09:10
  • @VinayGupta the alternate syntax only works for key-value *pairs*, e.g. `{ method: :delete }`. Otherwise, the colon comes always first. – Stefan Feb 05 '15 at 09:18
  • Very concise. Like this answer – wpp Feb 05 '15 at 09:52
15
Rails.application.config.session_store :cookie_store, key: '_blog_session'

session_store is a method that takes two "Arguments":

  • :cookie_store is a Symbol
  • key: '_blog_session' is actually a short way of writing a Hash.

(could also be session_store :cookie_store, { key: '_blog_session' })

Similarly for link_to "Delete", article, confirm: "Are you sure?", method: :delete

  • "Delete" is a string
  • article a variable
  • { confirm: '...', method: :delete } hash where confirm:, method: and :delete are Symbols again.

While Blog::Application :: is basically a namespace resolution operator. A way for you to address the Application class in the Blog module.

Hope this helps. Have a look at the documentation I referenced, it is explained rather nicely.

Community
  • 1
  • 1
wpp
  • 7,093
  • 4
  • 33
  • 65
2

:presence => true

presence: true

In the bottom example, the colon is saying, “Hey, I am pointing from presence to true.

Aaquib Jawed
  • 485
  • 4
  • 11