Is Hash Rocket deprecated?
The author of that blog post is being overly dramatic and foolish, the =>
is still quite necessary. In particular:
You must use the rocket for symbols that are not valid labels:
:$set => x
is valid but$set: x
is not. In Ruby 2.2+ you can get around this problem with quotes:'$set': x
will do The Right Thing.You must use the rocket if you use keys in your Hashes that aren't symbols, such as strings, integers or constants. For example,
's' => x
is valid but's': x
is something completely different.
You can kludge around the above in the obvious manner of course:
h = { }
h[:'where.is'] = 'pancakes house?'
# etc.
but that's just ugly and unnecessary.
The rocket isn't going anywhere without crippling Ruby's Hashes.
Ruby/Rails hash rockets Syntax
They are the same, it is just a matter of preferences.
I also asked myself why would we add this new syntax if we already have one? Well, Programming with Ruby implies that we are lazy and want to type the less possible caracters. So this new syntax allow us - lazy programmers - to write the same thing, minus 1 caracter!
But keep in mind some stuff, like the type of the keys for instance (Ruby 1.9.3):
> {a: 12}.class
=> Hash
> {:a => 12}.class
=> Hash
> {'a' => 12}.keys.first.class
=> String
> {a: 12}.keys.first.class
=> Symbol
Also, some declaration are illegal with the new syntax:
> { '1-2' => "something" }
=> {"1-2"=>"something"}
> { 1-2: "something" }
SyntaxError: (irb):38: syntax error, unexpected ':', expecting tASSOC
{ 1-2: "something" }
^
(irb):38: syntax error, unexpected '}', expecting $end
For more informations: Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
Ruby hash rockets vs 1.9 syntax
column
in that line of code isn't a symbol, its a variable, so you need to use the hash rocket. column: self[column]
would build a hash where the key was the symbol :column
, not the value of the variable column
, which is what you want.
The new syntax is just a shortcut when using a literal symbol for a key (key: value
instead of :key => value
). If you are using a variable key the =>
syntax is still required.
Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;
As the warning says, now you should implement the hash(into:)
function instead.
func hash(into hasher: inout Hasher) {
switch self {
case .mention: hasher.combine(-1)
case .hashtag: hasher.combine(-2)
case .url: hasher.combine(-3)
case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable
}
}
It would be even better (in case of enums and struct) to remove the custom hash(into:)
implementation (unless you need a specific implementation) as the compiler will automatically synthesize it for you.
Just make your enum conforming to it:
public enum ActiveType: Hashable {
case mention
case hashtag
case url
case custom(pattern: String)
var pattern: String {
switch self {
case .mention: return RegexParser.mentionPattern
case .hashtag: return RegexParser.hashtagPattern
case .url: return RegexParser.urlPattern
case .custom(let regex): return regex
}
}
}
Explain hash rocket in this context
that works although I am not entirely sure why
As long as
Sequel.qualify(:meeting_graphs, :id)
is valid, it can be a key of a hash. Any object can be a key of a hash. That is why.why it does this not work when I replace the hash rocket with a colon
Even if
Sequel.qualify(:meeting_graphs, :id)
turns out to be a symbol, the colon notation will not work because it is part of a literal notation. It is not a method or a keyword that works on Ruby objects that are already made.
Convert ruby source code from old style to new style hash
Rubocop is a static code analyzer that evaluates your code against many of the recommendations of the Ruby Style Guide. It has an --auto-correct
option that can automatically change your code to what is recommended. One of those auto-correct options is for hash syntax.
Example:
rubocop --only HashSyntax --auto-correct
will only correct your hashes.
Related Topics
How to Calculate Number of Chars Common to Two Strings
Class ≪≪ Self Idiom in Ruby
How to "Pretty" Format Json Output in Ruby on Rails
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
Rhc Setup Gives Error 'No Such File Dl/Import'
How to Check Whether a String Contains a Substring in Ruby
Rails Activerecord: Find All Users Except Current User
Creating Microsoft Word (.Docx) Documents in Ruby
Case Statement With Multiple Values in Each 'When' Block
Why Use Ruby'S Attr_Accessor, Attr_Reader and Attr_Writer
How to Update Ruby Gems from Behind a Proxy (Isa-Ntlm)
Best Practices With Stdin in Ruby
Tzinfo::Datasourcenotfound Error Starting Rails V4.1.0 Server on Windows
How to Specify a Local Gem in My Gemfile