From: shevegen@... Date: 2016-07-14T07:58:01+00:00 Subject: [ruby-core:76370] [Ruby trunk Feature#12586] Hash#sample Issue #12586 has been reported by Robert A. Heiler. ---------------------------------------- Feature #12586: Hash#sample https://bugs.ruby-lang.org/issues/12586 * Author: Robert A. Heiler * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Hello. class Array has .sample method. I tried to use .sample on a hash, just to return a random element (I needed to debug this in my key, from a list of hash-kept remote URLs where I will batch download these files via a ruby script). To my surprise, class Hash has no .sample. I am not sure why not, perhaps there is some logical reason, but perhaps it may also have been forgotten? I am using this code as workaround: hash = { :cat => 'Tom', :mouse => 'Jerry', :dog => 'Spike'} hash.to_a.sample(1).to_h # => {:mouse=>"Jerry"} hash.to_a.sample(1).to_h # => {:cat=>"Tom"} This works fine. I could also modify class Hash of course but I rather want to stick to main-ruby (and thus not modify core classes of ruby if I release my code via gems). I did not need any argument other than 1 to .sample() since I only needed to obtain one random hash key-value pair. If this was already discussed or suggested and rejected, feel free to close this issue request. Thanks! -- https://bugs.ruby-lang.org/ Unsubscribe: