In Python I can do a_set.pop()
and I remove an element from the set and catch it in a variable. Is there a way to do the same thing with a set in Ruby?
Ruby Set haven't pop
method, but you can do like this:
class Set
def pop
temp = self.to_a.pop
self.delete(temp)
temp
end
end
s1 = Set.new [1, 2]
=> #<Set: {1, 2}>
s1.pop
=> 2
s1
=> #<Set: {1}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With