Is Hash Rocket Deprecated

Is Hash Rocket deprecated?

The author of that blog post is being overly dramatic and foolish, the => is still quite necessary. In particular:

  1. 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.

  2. 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



Leave a reply



Submit