Ruby: Colon Before VS After

Ruby: colon before vs after

You are welcome for both, while creating Hash :

{:name => "foo"}
#or
{name: 'foo'} # This is allowed since Ruby 1.9

But basically :name is a Symbol object in Ruby.

From docs

Hashes allow an alternate syntax form when your keys are always symbols. Instead of

options = { :font_size => 10, :font_family => "Arial" }

You could write it as:

options = { font_size: 10, font_family: "Arial" }

What is the colon operator in Ruby?

:foo is a symbol named "foo". Symbols have the distinct feature that any two symbols named the same will be identical:

"foo".equal? "foo"  # false
:foo.equal? :foo # true

This makes comparing two symbols really fast (since only a pointer comparison is involved, as opposed to comparing all the characters like you would in a string), plus you won't have a zillion copies of the same symbol floating about.

Also, unlike strings, symbols are immutable.

ruby on rails, colon at back or front of variables

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

Significance of colon : in Ruby

A colon denotes a "symbol". A symbol is like a string, but it is immutable (you can't change its contents). Behind the scenes, it also takes up less memory, since a symbol only needs to exist once in memory (i.e., two strings called "length" will exist twice in memory, but two symbols called :length will point to the same object).

ruby code: why put colon in front of variable name (inside initialize method)

Those are keyword arguments.

You can use them by name and not position. E.g.

ThatClass.new(var1: 42, var2: "foo")

or

ThatClass.new(var2: "foo", var1: 42)

An article about keyword arguments by thoughtbot

Ruby confusion on the use of colons

In this case you are calling the function post with two parameters, the first parameter is the symbol :create and the second is a hash with the key :product and the value @update.

This line could be re-written as follows:

post(:create, {:product => @update})

The key: value style was introduced in Ruby 1.9.



Related Topics



Leave a reply



Submit