Created
February 3, 2019 22:06
-
-
Save RoUS/e1117ec2db98a7f73696147970875b17 to your computer and use it in GitHub Desktop.
Deconstructing arrays in block arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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