To get the value of a key in a hash in Ruby, you can use either the square bracket notation or the fetch method. Here are some examples:
# Using the square bracket notation
person = { name: "John", age: 30 }
puts person[:name] # Output: John
# Using the fetch method
person = { "name" => "Mary", "age" => 25 }
puts person.fetch("age") # Output: 25
In the first example, we defined a hash with two key-value pairs, and then accessed the value associated with the name key using the square bracket notation.
In the second example, we defined a hash with two key-value pairs, where the keys are strings. We then used the fetch method to access the value associated with the age key.
Both approaches are valid, but they have some differences. Using the square bracket notation will return nil if the key is not found, whereas using the fetch method will raise a KeyError if the key is not found (unless a default value is provided as an argument).