In Ruby What Is the Meaning of Colon After Identifier in a Hash

In Ruby what is the meaning of colon after identifier in a Hash?

The colon in this context denotes a literal Hash.

factory is the Hash key, :user is the value.

The alternative syntax is :factory => :user. They mean the same thing.

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.

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.

Colon placement in Ruby on Rails.

1) abc: it can't exist independently

2) :xyz it is a symbol

:xyz.class
=> Symbol

3) abc::xyz it represents namespace

Example code:

module ABC
class Xyz
def initialize
@size = 400
end
end
end

x = ABC::Xyz.new

4) abc: :xyz

hash = {abc: :xyz} #hash key and value all are symbol. 

5) abc: xyz

xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.

6) :abc => xyz

xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation

7) ternary operator :

abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2

What's the difference `{'x'= 3}` and `{x: 3}`?

In b,c,d, and e, the key is a Symbol.

In a, the key is a String.

a = { 'x' => 3 }  #=> { "x" => 3 } 
b = { 'x': 3 } #=> { :x => 3 }
c = { x: 3 } #=> { :x => 3 }
d = { :x => 3 } #=> { :x => 3 }
e = { :'x' => 3 } #=> { :x => 3 }

What are those x: identifiers called in rails?

It's just syntactic sugar.

{as: :file, wrapper: :vertical_file_input}

is a Hash, equal to

{:as=>:file, :wrapper=>:vertical_file_input}

I personally don't find the first version more readable.

So x: is just :x (a Symbol) as key in a hash.

f.input :XML, as: :file, wrapper: :vertical_file_input

is the method "input" called on object f, with :XML as first parameter and {:as=>:file, :wrapper=>:vertical_file_input} as second parameter. You could also write it :

f.input(:XML, {as: :file, wrapper: :vertical_file_input})

This kind of method call is very common in Rails.

What is the # (sharp, number, pound, hash) sign used for in Ruby?

This is how instance method described:

Array#fill 

So you can:

a = Array.new(2)
=> [nil, nil]
a.fill(42)
=> [42, 42]

This is how class method described:

String::new

s = String.new('abc')
=> "abc"

Understanding Ruby variables and symbols?

Variables starting with @ are instance variables, "properties" in other languages. Whereas 'classic' variables are local to the scope of their method/block, instance variables are local to a specific instance of an object, for example:

class Foo

def initialize(bar)
@bar = bar
end

def bar
@bar # the variable is specific to this instance
end

def buzz
buzz = 'buzz' # this variable is not accessible outside of this method
end

end

You may also see variables starting with @@, which are class variables, and are accessible by every instance of the class and shared with every instance of the subclass. Usage of those variables is usually discouraged, primarily because subclasses share the variable, which can cause a lot of mess.

In Ruby everything is an object, classes are objects (instances of class Class), so you can also have class instance variables:

class Foo

def self.bar
@bar #we are in class Foo's scope, which is an instance of class Class
end

def self.bar=(bar)
@bar = bar
end

def bar
@bar # Foo.new.bar != Foo.bar
end

end

What you call "variables with a colon" are not variables. They are a particular type of string, called a symbol, that is immutable and optimized for quick identification by the interpreter, in fact, those are stored internally as pointers, so that :this == :this is a very quick operation.

This property makes them good candidates for hash keys because they offer quick retrieval or for "flags" to pass to a method; Think of them as a sort of loose constant that "stands for" what they say. Their immutability is also dangerous: All symbols ever created never get garbage collected; It's easy to create a memory-leak by creating thousands of symbols, so use them wisely.

UPDATE since ruby 2.2 symbols may be garbage-collected in certain cases (when no reference is kept and no comparison is needed)

How do I access the value for this nested hash's key in Ruby?

If you have:

h = {:charge_payable_response=>{:return=>"700", :ns2=>"http://ws.myws.com/"}}

Then use:

h[:charge_payable_response][:return]
# => "700"

The colon prefix means that the key in the hash is a symbol, a special sort of unique identifier:

Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods. The same Symbol object will be created for a given name or string for the duration of a program‘s execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.



Related Topics



Leave a reply



Submit