[#76442] [Ruby trunk Feature#11741] Migrate Ruby to Git from Subversion — naruse@...
Issue #11741 has been updated by Yui NARUSE.
3 messages
2016/07/19
[#76515] [Ruby trunk Bug#12610] webrick: protect from httpoxy — nagachika00@...
Issue #12610 has been updated by Tomoyuki Chikanaga.
3 messages
2016/07/22
[ruby-core:76370] [Ruby trunk Feature#12586] Hash#sample
From:
shevegen@...
Date:
2016-07-14 07:58:01 UTC
List:
ruby-core #76370
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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>