Skip to content

Instantly share code, notes, and snippets.

@RoUS
Created February 3, 2019 22:06
Show Gist options
  • Save RoUS/e1117ec2db98a7f73696147970875b17 to your computer and use it in GitHub Desktop.
Save RoUS/e1117ec2db98a7f73696147970875b17 to your computer and use it in GitHub Desktop.
Deconstructing arrays in block arguments
h1 = {
:one => 1,
:two => 2,
:three => 3,
}
# h1 => { :one => 1, :two => 2, :three => 3 }
#
# Each key/value pair is passed as a two-element array as the second
# block argument -- so you can deconstruct it in the arglist declaration.
#
h2 = h1.inject({}) { |memo,(key,val)|
memo[key.to_s] = val
memo
}
# h2 => { "one" => 1, "two" => 2, "three" => 3 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment