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
152: Rightward Assignment
I've seen this mistake a hundred times, usually from developers coming from languages where you're taught to be very explicit about every single memory move. You're trying to swap two values, and you think you've got it handled, but your data just... disappears.
# We want to swap the priority of two tasks
high_priority = "Fix production bug"
low_priority = "Update documentation"
high_priority = low_priority
low_priority = high_priority
puts "High: #{high_priority}" # Output: Update documentation
puts "Low: #{low_priority}" # Output: Update documentation
The Vanishing Variable
If you run that code, you'll notice that "Fix production bug" is gone entirely. Both variables now point to the documentation task. Why? Because Ruby executes these assignments sequentially. In the first line, you told Ruby to make high_priority point to the same object as low_priority. By the time you got to the second line, the original value of high_priority was already overwritten. It's gone.
In other languages, you'd have to create a temp variable to hold the value of high_priority while you move things around. It's clunky, it's three lines of code, and it's an easy place to make a typo.
Swapping in One Breath
Ruby gives us a much more elegant way to handle this called parallel assignment, or what some call rightward assignment. You can handle the swap in a single line:
high_priority = "Fix production bug"
low_priority = "Update documentation"
high_priority, low_priority = low_priority, high_priority
puts "High: #{high_priority}" # Output: Update documentation
puts "Low: #{low_priority}" # Output: Fix production bug
I love this syntax because it reads like a mathematical statement. But it's worth knowing what's actually happening under the hood so you don't treat it like magic. Ruby isn't actually doing two assignments at the exact same nanosecond. Instead, it evaluates the right side of the equals sign first. It creates a temporary array ["Update documentation", "Fix production bug"] and then "unpacks" that array into the variables on the left.
Unpacking Method Results
Parallel assignment isn't just for swapping. It's incredibly powerful when a method returns multiple values (which, in Ruby, usually means it's returning an array). Instead of grabbing the array and accessing indices like result[0] and result[1], you can assign them directly.
Take a look at how I'd handle a coordinate system:
def get_player_position
[10, 42] # Returns an array representing x and y
end
x, y = get_player_position
puts "Player is at X: #{x}, Y: #{y}"
It's cleaner, it's more descriptive, and it removes the cognitive load of remembering which index corresponds to which axis. Just be careful: if the method returns more values than you have variables, the extras are simply ignored. If it returns fewer, the remaining variables will be assigned nil.
📋 Practical Task
Refactoring the Game State Swap
You are working on a turn-based strategy game. Currently, the code for swapping the "Active Player" and the "Waiting Player" is using a clunky temporary variable. Your goal is to refactor this using parallel assignment to make the code more "Ruby-esque".
Your Task:
Modify the swap_turns method in the code below. Remove the temp_player variable and replace the three-line swap logic with a single line of parallel assignment.
class Game
attr_accessor :active_player, :waiting_player
def initialize
@active_player = "Player 1"
@waiting_player = "Player 2"
end
def swap_turns
# REFACTOR THIS SECTION
temp_player = @active_player
@active_player = @waiting_player
@waiting_player = temp_player
# END REFACTOR
end
end
game = Game.new
puts "Active: #{game.active_player}" # Player 1
game.swap_turns
puts "Active: #{game.active_player}" # Should be Player 2
There are no comments for now.