[#71439] [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API — matz@...
Issue #11339 has been updated by Yukihiro Matsumoto.
7 messages
2015/11/11
[#71473] Re: [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/11/13
Entire series for sockets
[#71450] Ruby 2.3.0-preview1 Released — "NARUSE, Yui" <naruse@...>
Hi,
5 messages
2015/11/11
[#71617] [Ruby trunk - Feature #11664] [PATCH] introduce rb_autoload_value to replace rb_autoload — nobu@...
Issue #11664 has been updated by Nobuyoshi Nakada.
3 messages
2015/11/20
[#71721] [Ruby trunk - Feature #11741] Migrate Ruby to Git from Subversion — me@...
Issue #11741 has been updated by Jon Moss.
4 messages
2015/11/28
[ruby-core:71510] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
From:
6ftdan@...
Date:
2015-11-17 02:10:15 UTC
List:
ruby-core #71510
Issue #11690 has been updated by Daniel P. Clark.
Nobuyoshi Nakada wrote:
> Just an idea.
>
> ~~~ruby
> class ToBeAssigned
> def initialize(obj)
> @target = obj
> end
> def method_missing(m, *args)
> @target.__send__(m.to_s.chomp('='), *args)
> end
> def respond_to_missing?(m)
> @target.respond_to?(m.to_s.chomp('='))
> end
> end
> module Kernel
> def to_be_assigned_with
> ToBeAssigned.new(self)
> end
> end
>
> x = {a: 1, b: 2}
>
> x.to_be_assigned_with.update, y ,z = {c: 3}, 6, 7
> p x
> ~~~
I absolutely love the extensibility of this in that it can be used on any object to allow multiple assignment with any method. Brilliant Nobuyoshi! Now if only the method name wasn't so long ;-)
Maybe we could replace `to_be_assigned_with` to `slurp`, `consume`, `devour`, or `injest` ;-) It properly expresses the additive nature of the action. Oooh! Or even `feed_me`. eg) `x.feed_me.update` ... I don't think most of those would make it in as an official method in Ruby though. They're a bit silly.
----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54882
* Author: Daniel P. Clark
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
Given that we can assign multiple variables at once
~~~ruby
a,b,c = 1,2,3
~~~
It would be nice to be able to update a Hash during multiple assignment rather than replacing it. Currently
~~~ruby
x = {a: 1, b: 2}
x, y ,z = {c: 3}, 6, 7
x
# => {c: 3}
~~~
What I propose is adding `Hash#update=` to permit updating during multiple assignment.
~~~ruby
class Hash
def update=(h)
update(h)
end
end
x = {a: 1, b: 2}
x.update, y ,z = {c: 3}, 6, 7
x
# => {a: 1, b: 2, c: 3}
~~~
This would be most useful in scenarios where a method or proc return multiple values. When the method returns the values we don't normally know the key outside where the hash assignment is.
~~~ruby
example = proc { [{:hi => :hello}, 5] }
hash = {}
# Currently in Ruby with an Unknown key multiple assignment isn't an option
hash[????], current = example.call
# We currently have to two step it
result, current = example.call
hash.update(result)
~~~
But with `Hash#update=` we don't have to know the key.
--
https://bugs.ruby-lang.org/