Skip to content

Instantly share code, notes, and snippets.

@eduard-io
Created May 23, 2012 18:11
Show Gist options
  • Save eduard-io/2776761 to your computer and use it in GitHub Desktop.
Save eduard-io/2776761 to your computer and use it in GitHub Desktop.
Remove last element
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]
@eduard-io
Copy link
Author

one liner is b0rked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment