-3
aaa = { :one => "eins", :two => "zwei", :three => "drei" }
bbb = { one: "eins", two: "zwei", three: "drei" }

above are valid ruby code. at line 1, why there is ":" before "one"? What's the meaning?

Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129
  • 2
    https://ruby-doc.org/core-3.0.0/Hash.html, https://ruby-doc.org/core-3.0.0/Symbol.html – axiac Mar 06 '21 at 15:40
  • Also [**Is there any difference between the `:key => “value”` and `key: “value”` hash notations?**](https://stackoverflow.com/q/8675206/479863) – mu is too short Mar 06 '21 at 17:38

2 Answers2

1

It's called Symbol, you could think of it as a special string. Symbols are often used as hash keys.

You could check out more on Ruby Symbols vs. Strings

eux
  • 3,072
  • 5
  • 14
0

In ruby hash is an association of key and value.

my_hash = { :key => "value" }

or

my_hash = { key: "value" }

more informations here : https://launchschool.com/books/ruby/read/hashes

ffouquet42
  • 124
  • 13