[#50113] k0kubunさんをERBのコミッタに推薦します — Masatoshi SEKI <m_seki@...>
咳といいます。
5 messages
2017/05/13
[ruby-dev:50115] [Ruby trunk Feature#13563] Implement Hash#choice method.
From:
shevegen@...
Date:
2017-05-14 17:57:19 UTC
List:
ruby-dev #50115
Issue #13563 has been updated by shevegen (Robert A. Heiler).
I am neutral about the suggestion, neither pro or con.
I think the only thing that I want to point out is that .choice() is perhaps not
completely clear. Perhaps .pick() would be better?
We have quite some methods already that allow us to .select() .reject(); and
it also reminds me a bit about .shuffle() and .sample().
I guess this is not the fault of anyone by the way, we just do not have that
many (one-word) english words that are for verbs/actions. So perhaps .choice()
is a good name anyway.
----------------------------------------
Feature #13563: Implement Hash#choice method.
https://bugs.ruby-lang.org/issues/13563#change-64808
* Author: babanba-n (matzbara masanao)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
Hi,
I propose Hash#choice method.
It pick up key and value pairs in Hash like a below code.
~~~
{ :a => 1, 2 => "2", "c" => true }.choice(:a, 2) # => { :a => 1, 2 => "2" }
{ :a => 1, 2 => "2", "c" => true }.choice("c", 10000) # => { "c" => true, 10000 => nil }
~~~
This method is useful when Hash have many keys, but programer need few key's.
For instance, it pick up personal data in ActiveRecord model's data, and migrate to new device.
~~~
feature_phone_user = User.find(params[:src_user_id])
profile = feature_phone_user.attributes.choice(*%i[nickname email sex birthday prefecture])
smart_phone_user = User.new
smart_phone_user.attributes = profile
~~~
In other case, it pick up latest log from http request parameter.
~~~
condition = params.to_h.choice("user_id", "service_id")
latest_payment_log = PaymentLog.find_by(condition).last
~~~
If this method exist, I guess that many cases change to comfortable just a little.
Please consider about it.
And I write it.
https://github.com/baban/ruby/commit/4014ebe2ae261881669bef8369eb08aaecef2034
--
https://bugs.ruby-lang.org/