ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:105579] [Ruby master Feature#7793] New methods on Hash
       [not found] <redmine.issue-7793.20130207015307.124@ruby-lang.org>
@ 2021-10-06 20:20 ` jeremyevans0 (Jeremy Evans)
  0 siblings, 0 replies; only message in thread
From: jeremyevans0 (Jeremy Evans) @ 2021-10-06 20:20 UTC (permalink / raw)
  To: ruby-core

Issue #7793 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Assigned to Closed

This feature was implemented as `transform_keys`, `transform_values`, and `to_h`.

----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-94039

* Author: dsisnero (Dominic Sisneros)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
It would be nice to have the following methods added to hash

~~~ruby
h = { name: 'dominic', request: 'add the following methods', :why => 'convenience'}

h.map_v{|v| v.upcase}
#=> {:name=>"DOMINIC", :request=>"ADD THE FOLLOWING METHODS", :why=>"CONVENIENCE"}

h.map_k{|k| k.to_s}
#=> { "name"=> 'dominic', "request"=>"add the following methods', "why" => "convenience"}

h.map_kv{|k,v| [k.to_s, v.upcase]}
#=> { "name"=>"DOMINIC", "request"=>"ADD THE FOLLOWING METHODS", "why"=>"CONVENIENCE"}


class Hash

  def map_v
    reduce({}) do |result, array|
      k,v = array
      new_val = yield v
      result.merge( k => new_val)
    end
  end

  def map_k
    reduce({}) do |result, array|
      k,v = array
      new_k = yield k
      result.merge(new_k => v)
    end
  end

  def map_kv
    reduce({}) do |result, array|
      new_k,new_v = yield array
      result.merge(new_k => new_v)
    end
  end

end




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-06 20:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-7793.20130207015307.124@ruby-lang.org>
2021-10-06 20:20 ` [ruby-core:105579] [Ruby master Feature#7793] New methods on Hash jeremyevans0 (Jeremy Evans)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).