0

How do I use the new hash style with numbers as keys?

I have:

 { 1 => "terrible", 3=> "OK", 5 => "awesome" }

But I'd like to know how to write them using the new hash style.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Cleyton
  • 2,338
  • 6
  • 23
  • 39
  • 3
    You can't. There is no such thing as a "new hash syntax" for numeric keys. Ruby has introduced a new syntax explicitly for using symbols as keys. Any amount of reading should have given you this answer. – user229044 Oct 03 '14 at 19:57
  • @meagar: *Certain* symbols, you can't use the JavaScript-style notation with symbols like `:$set`, `:'0'`, ... Yes, this is my pet peeve. – mu is too short Oct 03 '14 at 20:27
  • Yes, this is reserved for keys that are symbols, but could the designers of that feature have made it more general? Suppose `key: value` were interpreted as `{ key => value }` if `key.is_a? Object => true`, and as `{ :key => value }` if `key.is_a? Object` raised an "undefined local variable or method" exception. (What about `key.is_a? Object => false`?). Not advocating, just mulling. – Cary Swoveland Oct 03 '14 at 20:41

2 Answers2

1

You don't, to use the new syntax you must use a subset of valid symbols. Numbers aren't that, and as Mu points out, there are other restrictions as well.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

Your key is a number, not a symbol. With the new hash style of {key: value} the key is always supposed to be a symbol, so you can't use the new hash style with your hash because your keys start with numbers, not symbols. See "Update your entire project to Ruby 1.9 hash syntax" where it explicitly states:

In version 1.9 Ruby introduced new syntax for hash literals whose keys are symbols.

daremkd
  • 8,244
  • 6
  • 40
  • 66
  • 1
    Technically symbols *can* start with a number; `:"123"` and `"123".to_sym` both produce a symbol composed of digits only. – user229044 Oct 03 '14 at 20:01
  • Something like a = :1 will produce an error. What you're describing is a string data type, not a number. – daremkd Oct 03 '14 at 20:03
  • Instead of saying "this link", use something descriptive for anchor text. See the W3's "[Link text](http://www.w3.org/TR/WCAG10-HTML-TECHS/#link-text)" section, and "[Don't use "click here" as link text](http://www.w3.org/QA/Tips/noClickHere)" – the Tin Man Oct 03 '14 at 20:06
  • @daremkd What meagar was describing are actually Symbols, however are you note those are not supported in the new Hash syntax. `:"123".class # => Symbol`. – Daniël Knippers Oct 03 '14 at 20:10
  • 1
    @DaniëlKnippers FYI, that syntax will be supported for hashes in the next version of Ruby: https://bugs.ruby-lang.org/issues/4276 – Ajedi32 Oct 03 '14 at 20:15
  • You can wrap the numbers in double quotes and do :"Anything goes here" but that doesn't change the fact at the end they'll be symbols. I think there's a difference between the DATA TYPE and the CONTENT in the actual symbol data type. His question was whether he can use the new hash style with numbers as keys and the answer is not. Symbols types can be the only type of keys with the 1.9 new hash style. – daremkd Oct 03 '14 at 20:18