How can I pop an element from a Set in Ruby? -


in python can a_set.pop() , remove element set , catch in variable. there way same thing set in ruby?

the best way research question @ docs. google search 'ruby set' find doc ruby set class. can review of available instance methods.

as can see, set "a collection of unordered values no duplicates." since it's unordered, it's not designed values @ particular locations within set, including last value.

you convert array using #to_a , use #pop on array, since order of set not guaranteed, might not end value you're expecting.

edit: interestingly, pop in python deletes , returns "an arbitrary element." see that's looking for. while not available built-in method in ruby set, suppose implement using array#sample, grabs random element array, , delete set:

class set   def pop     el = self.to_a.sample     self.delete(el)     el   end end 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -