Reason #79 • March 20th, 2026

Hash#values_at

Hash#values_at is great when we want a fixed subset of fields in a fixed order. It is typically used together with positional assignments to unpack values into separate variables:

Ruby
account = {
  id: 42,
  country: "Sweden",
  plan: "pro",
  created_at: "2024-01-15",
  active: true
}

# Pluck specific keys
id, country, active = account.values_at(:id, :country, :active)
# id => 42
# country => "Sweden"
# active => true

# Create a CSV row from a subset of fields
csv_row = account.values_at(:id, :country, :plan).join(",")
# => "42,Sweden,pro"
      
JavaScript
const account = {
  id: 42,
  country: "Sweden",
  plan: "pro",
  created_at: "2024-01-15",
  active: true
};

// Pluck specific keys
const { id, country, active } = account;
// id => 42
// country => "Sweden"
// active => true

// Create a CSV row from a subset of fields
const csvRow = [account.id, account.country, account.plan].join(",");
// => "42,Sweden,pro"
      

Not gonna lie, I do love the destructuring assignment syntax in JavaScript for this as well. Both values_at and destructuring make for concise code when we want to turn fields into local variables. The CSV case favors values_at though since we pluck the values directly into an Array.

It is also possible to do destructuring in Ruby via "rightward assignment", though this style is less common to spot in the wild:

Ruby
account => { id:, country:, active: }

# id => 42
# country => "Sweden"
# active => true
    

A discrepancy when using => for destructuring is that it raises NoMatchingPatternKeyError when keys are missing, while values_at will fall back to nil for missing keys. So if we need to be resilient to missing fields, values_at is the safer bet.

History

Hash#values_at was added in Ruby 1.8, released in 2003.

Rightward assignment via pattern matching was introduced much later in Ruby 3.0, released in 2020.

JavaScript got its destructuring assignment functionality in 2015.

Perhaps both JavaScript's destructuring and Ruby's pattern matching were influenced by similar concepts in functional programming languages like Haskell and Elixir.

Reason #80 ?