Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
153: Hash Shorthand Syntax
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
There are no comments for now.