When we're defining a Hash in Ruby, there's several syntax we can use:
# IRB Ruby 2.7.1 - Defining a Hash my_hash = { 'string_transformed_into_symbol_1': 'value', "string_transformed_into_symbol_2": 'value', 'string_1' => 'value', "string_2" => 'value', symbol_1: 'value', :symbol_2 => 'value' } I was thinking that the arrow => was from Ruby 1.8 and was replaced by the colon :. Though, from the example above, we can see that it is not only a syntactic sugar: : transforms a key String into Symbol:
# IRB Ruby 2.7.1 - Outputing a Hash my_hash => { :string_transformed_into_symbol_1 => "value", :string_transformed_into_symbol_2 => "value", "string_1" => "value", "string_2" => "value", :symbol_1 => "value", :symbol_2 => "value" } From this question, mu is too short's great answer stated:
The JavaScript style (
key: value) is only useful if all of your Hash keys are "simple" symbols (more or less something that matches/\A[a-z_]\w*\z/i[...] But you still need the hashrocket if your keys are not symbols.
This makes me consider : and => as methods (but I know I'm wrong). However, I couldn't find anything on the documentation about it.
Q: What exactly are : and => ? How could I learn more about them, and where are they defined in Ruby's source code ?