What's the difference between => and : for hash keys in Ruby?
delegate :sum, to: :CONSTANT_ARRAY
delegate :sum, :to => :CONSTANT_ARRAY
Why would one be better than another?
What's the difference between => and : for hash keys in Ruby?
delegate :sum, to: :CONSTANT_ARRAY
delegate :sum, :to => :CONSTANT_ARRAY
Why would one be better than another?
=>
is more general than just :
. With =>
the key can be any data type, whereas using :
forces the key to be a symbol.
For example:
{ a: 1 }
# => {:a=>1} # The key is the symbol :a
{ 'a' => 1 }
# => {"a"=>1} # The key is the string "a"
{ "a": 1 }
# => SyntaxError
Why would one be better than another?
If your keys are symbols then you may find the :
syntax to be clearer and less cluttered. It is also immediately recognizable to those who are familiar with the JSON data format (though technically the key must be a string surrounded by double-quotes (") according to the standard to be valid JSON syntax, but the non-quoted keys are recognized in many languages including JavaScript.)
There's no difference.
It depends on your preference.
delegate :sum, to: :CONSTANT_ARRAY
is the Ruby 1.9 and after syntax.
delegate :sum, :to => :CONSTANT_ARRAY
is the Ruby 1.8 syntax.
Basically, in this case, there's no difference.
The Hash-Rocket =>
is the old style, probably borrowed from perl, while the colon syntax is the newer, ruby 1.9 style of writing hashes.
The latter is more directed towards json-notation and therefore might be easier to read if one is more familiar with it.