Skip to Content
Course content

153: Hash Shorthand Syntax

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, why can I just put a colon after the variable name now?

If you've been looking at newer Ruby code (version 3.1 and up), you've probably seen something that looks like a syntax error: a hash where the value is just... missing. It's called "Value Omission."

Basically, when you're creating a hash and the key is a symbol that happens to have the exact same name as the variable you're assigning to it, Ruby lets you skip the redundant part. I used to spend way too much time typing name: name, email: email, phone: phone. It felt like I was stuttering in code.

# The old, repetitive way
user_name = "Alice"
user_role = "Admin"

profile = { name: user_name, role: user_role } # This is fine, but verbose
# But if the variables matched the keys...
name = "Alice"
role = "Admin"
profile = { name: name, role: role } 

With the shorthand, if your variable names match your keys, you just do this:

name = "Alice"
role = "Admin"

# Ruby 3.1+ shorthand
profile = { name:, role: }

Ruby sees name: and says, "I bet there's a local variable called name nearby," and it automatically plugs that value in. It's a small win, but when you're passing ten different parameters into a service object, it cleans up the visual noise significantly.

Does this work for any kind of key, like strings?

Nope. This is strictly for symbol keys. If you're using the older hash rocket syntax or string keys, you're stuck with the full assignment.

name = "Alice"

# This will NOT work:
# { "name" => : } # Syntax Error!

# This ONLY works with the symbol-label syntax:
{ name: } 

I wouldn't worry about this too much, though. In modern Ruby development—especially if you're working in Rails—symbol keys are the standard for hashes anyway. You'll rarely find a reason to use string keys for internal data structures where this shorthand would be useful.

Is there any catch? Like, will it crash if the variable doesn't exist?

Yes, it will. It doesn't just return nil if it can't find the variable; it throws a NameError. It's behaving exactly as if you had typed the variable name explicitly.

# If I haven't defined 'age' yet...
user = { name: "Alice", age: } 
# => NameError: undefined local variable or method `age'

One thing I've noticed is that this can occasionally make a hash look too empty if you have a lot of shorthands, which might confuse a junior dev who isn't up to date on Ruby 3.1. But honestly, once you've used it for a week, you'll find it much easier to scan. You're seeing the keys, and you already know the values are coming from the local scope.




📋 Practical Task

Refactoring the GameCharacter State Hash

You are working on a RPG engine. You have a method that captures the current state of a character to save it to a database. The current implementation is using the old, verbose hash syntax. Refactor the save_character_state method to use Ruby 3.1+ hash shorthand for all applicable keys.

def save_character_state
  # Local variables representing current state
  name = "Grog the Barbarian"
  level = 14
  health = 120
  stamina = 80
  location = "Ironforge"

  # TODO: Refactor this hash to use shorthand syntax
  {
    name: name,
    level: level,
    health: health,
    stamina: stamina,
    location: location
  }
end

puts save_character_state
Rating
0 0

There are no comments for now.

to be the first to leave a comment.