Created
May 23, 2012 18:11
-
-
Save eduard-io/2776761 to your computer and use it in GitHub Desktop.
Remove last element
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
h = {a: 1, b: 2, c:3} | |
# => {:a=>1, :b=>2, :c=>3} | |
a = h.flatten | |
# => [:a, 1, :b, 2, :c, 3] | |
a.reverse! | |
# => [3, :c, 2, :b, 1, :a] | |
a.shift(2) | |
# => [3, :c] | |
a | |
# => [2, :b, 1, :a] | |
new_h = Hash[*a.reverse] | |
# => {:a=>1, :b=>2} | |
h | |
# => {:a=>1, :b=>2, :c=>3} | |
new_h | |
# => {:a=>1, :b=>2} | |
# O en una linea :P | |
Hash[*h.flatten.reverse.shift(2).reverse] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one liner is b0rked