ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:71495] [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
@ 2015-11-15 16:30 ` 6ftdan
  2015-11-15 21:04   ` [ruby-core:71499] " Matthew Kerwin
  2015-11-16 14:17 ` [ruby-core:71507] [Ruby trunk - Feature #11690] " 6ftdan
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 12+ messages in thread
From: 6ftdan @ 2015-11-15 16:30 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been reported by Daniel P. Clark.

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71499] Re: [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment
  2015-11-15 16:30 ` [ruby-core:71495] [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment 6ftdan
@ 2015-11-15 21:04   ` Matthew Kerwin
  0 siblings, 0 replies; 12+ messages in thread
From: Matthew Kerwin @ 2015-11-15 21:04 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 1026 bytes --]

On 16/11/2015 2:32 AM, <6ftdan@gmail.com> wrote:
>
> 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.
>

I get the use-case, but I think the understandability of the code starts to
suffer.

What about something completely new but splat-like?

    hash[*], current = example.call

This is even better when the hash comes last, it looks more like an options
parameter:

    opts = get_default_hash
    a, b, opts[*] = example2.call

I would assume this also works in single assignment:

    opts = get_default_hash
    opts[*] = get_new_opts

[-- Attachment #2: Type: text/html, Size: 1398 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71507] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
  2015-11-15 16:30 ` [ruby-core:71495] [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment 6ftdan
@ 2015-11-16 14:17 ` 6ftdan
  2015-11-16 21:31   ` [ruby-core:71508] " Matthew Kerwin
  2015-11-17  1:29 ` [ruby-core:71509] " nobu
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 12+ messages in thread
From: 6ftdan @ 2015-11-16 14:17 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


I'm not sure the opts[*] is a good idea.  You have have current splat behavior with multiple assignment.

~~~ruby
a, *, c = [1,2,3,4,5]
a
# => 1
c
# => 5
~~~

And the opts[*] is unclear about what it is and what it expects.  It could be a Proc.

~~~ruby
prc = proc {|b,c,d| b+c+d }
# if [*] were implemented
a, prc[*], c = [1,2,3,4,5]
~~~

As far as I know **Hash** is the only core object with an **update** method.  And in other cases where **update** is used, such as in Rails' ActiveRecord, a Hash is the expected input.  So when the **update** method is called it is generally understood to be expecting a **Hash** object.

Implementing [*] would be much more work that simply implementing `Hash#update=`

I was thinking about suggesting `Hash#merge!=` as well but Ruby syntax does not allow the equals sign after and exclamation point.  So I have chosen to only suggest `Hash#update=`

~~~ruby
class Hash
  def update=(h)
    update(h)
  end
end
~~~

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54879

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71508] Re: [Ruby trunk - Feature #11690] Update Hash during multiple assignment
  2015-11-16 14:17 ` [ruby-core:71507] [Ruby trunk - Feature #11690] " 6ftdan
@ 2015-11-16 21:31   ` Matthew Kerwin
  0 siblings, 0 replies; 12+ messages in thread
From: Matthew Kerwin @ 2015-11-16 21:31 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 1435 bytes --]

On 17/11/2015 12:18 AM, <6ftdan@gmail.com> wrote:
>
> Issue #11690 has been updated by Daniel P. Clark.
>
>
> I'm not sure the opts[*] is a good idea.  You have have current splat
behavior with multiple assignment.
>
> ~~~ruby
> a, *, c = [1,2,3,4,5]
> a
> # => 1
> c
> # => 5
> ~~~
>
> And the opts[*] is unclear about what it is and what it expects.  It
could be a Proc.
>
> ~~~ruby
> prc = proc {|b,c,d| b+c+d }
> # if [*] were implemented
> a, prc[*], c = [1,2,3,4,5]
> ~~~
>

Good point. I won't suggest anything to do with hash-splatting **  ;)

> As far as I know **Hash** is the only core object with an **update**
method.  And in other cases where **update** is used, such as in Rails'
ActiveRecord, a Hash is the expected input.  So when the **update** method
is called it is generally understood to be expecting a **Hash** object.
>
> Implementing [*] would be much more work that simply implementing
`Hash#update=`
>
> I was thinking about suggesting `Hash#merge!=` as well but Ruby syntax
does not allow the equals sign after and exclamation point.  So I have
chosen to only suggest `Hash#update=`
>
> ~~~ruby
> class Hash
>   def update=(h)
>     update(h)
>   end
> end
> ~~~
>

It's still very strange to think of 'update' as a property of a hash
object. Without some kind of new syntactic construct, with its own clear
semantics, this seems like a hack and doesn't feel right in the core (to
me; others may disagree.)

[-- Attachment #2: Type: text/html, Size: 1881 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71509] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
  2015-11-15 16:30 ` [ruby-core:71495] [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment 6ftdan
  2015-11-16 14:17 ` [ruby-core:71507] [Ruby trunk - Feature #11690] " 6ftdan
@ 2015-11-17  1:29 ` nobu
  2015-11-17  2:10 ` [ruby-core:71510] " 6ftdan
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: nobu @ 2015-11-17  1:29 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Nobuyoshi Nakada.


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
~~~

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54881

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71510] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-11-17  1:29 ` [ruby-core:71509] " nobu
@ 2015-11-17  2:10 ` 6ftdan
  2015-11-17  3:06 ` [ruby-core:71514] " nobu
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-11-17  2:10 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71514] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-11-17  2:10 ` [ruby-core:71510] " 6ftdan
@ 2015-11-17  3:06 ` nobu
  2015-11-17 11:41 ` [ruby-core:71519] " 6ftdan
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: nobu @ 2015-11-17  3:06 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Nobuyoshi Nakada.


Daniel P. Clark wrote:
> Maybe we could replace `to_be_assigned_with` to `slurp`, `consume`, `devour`, or `injest` ;-)

My dictionary doesn't have the last word, maybe a compound word from `inject` and `ingest`?


----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54886

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71519] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-11-17  3:06 ` [ruby-core:71514] " nobu
@ 2015-11-17 11:41 ` 6ftdan
  2015-11-18 18:41 ` [ruby-core:71562] " 6ftdan
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-11-17 11:41 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


You're right!  I misspelled it. If I had added a space "in jest" it would be a joke.  `ingest` is what I had meant to say.

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54895

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71562] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-11-17 11:41 ` [ruby-core:71519] " 6ftdan
@ 2015-11-18 18:41 ` 6ftdan
  2015-11-18 19:13 ` [ruby-core:71563] " 6ftdan
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-11-18 18:41 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


I was thinking of what word(s) may best clarify this and not conflict with other potential uses.  One issue about the word `assigned` is it seems like `self = ` or `replace`.  I at first was thinking of `to_be` ... but that's sounds more like a promise of the future and each method would have to end in `ed` like so `x.to_be.updated = a: 1`.  To avoid using a promise form and complex `ed` handling I was thinking of just `to`; `x.to.update`.  But I don't think this is clear enough.  So I have found the shortest thing I can think of that matches the statement `to_be_assigned_with`, and that is `apply`.  It's short, simple, clear, and reveals to anyone that more is being done here.

To copy from Nobuyoshi's idea

~~~Ruby
class Applicable
  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 apply
    Applicable.new(self)
  end
end

x = {a: 1, b: 2}

x.apply.update, y ,z = {c: 3}, 6, 7
p x
~~~

Arguably I think this is just as clear as `to_be_assigned_with`, and is different enough for people to notice that something else is going on here.  That should be enough.

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54944

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71563] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2015-11-18 18:41 ` [ruby-core:71562] " 6ftdan
@ 2015-11-18 19:13 ` 6ftdan
  2015-11-22 23:29 ` [ruby-core:71635] " 6ftdan
  2015-12-10 12:26 ` [ruby-core:72030] " 6ftdan
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-11-18 19:13 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


If we wanted to avoid chomping the equals sign on methods that have the equals already we'd have to write in a check for that.  But I'm not sure that would be as performant.

~~~ruby
class Applicable
  def initialize(obj)
    @target = obj
  end
  def method_missing(m, *args)
    m = @target.respond_to?(m) ? m : m.to_s.chomp('=')
    @target.__send__(m, *args)
  end
  def respond_to_missing?(m)
    @target.respond_to?(m.to_s.chomp('='))
  end
end

x = {a: 1, b: 2}

x.apply.update, y ,z = {c: 3}, 6, 7

# Hash still responds to :[]= method now after :apply
x.apply[:z] = 0

x
# => {:a=>1, :b=>2, :c=>3, :z=>0}

~~~

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-54945

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:71635] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2015-11-18 19:13 ` [ruby-core:71563] " 6ftdan
@ 2015-11-22 23:29 ` 6ftdan
  2015-12-10 12:26 ` [ruby-core:72030] " 6ftdan
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-11-22 23:29 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


Even shorter `Kernel#with`

~~~ruby
my_hash = {}
my_hash.with.update, b, c = ->{ {a: 1}, 5, 6 }.call
~~~

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-55035

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:72030] [Ruby trunk - Feature #11690] Update Hash during multiple assignment
       [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2015-11-22 23:29 ` [ruby-core:71635] " 6ftdan
@ 2015-12-10 12:26 ` 6ftdan
  9 siblings, 0 replies; 12+ messages in thread
From: 6ftdan @ 2015-12-10 12:26 UTC (permalink / raw)
  To: ruby-core

Issue #11690 has been updated by Daniel P. Clark.


I made a gem for this. [gem assign](https://github.com/danielpclark/assign)

I figure Kernel#assign is the best method name for this and the object it returns is an Assignable object.

----------------------------------------
Feature #11690: Update Hash during multiple assignment
https://bugs.ruby-lang.org/issues/11690#change-55442

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-12-10 11:55 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11690.20151115163053@ruby-lang.org>
2015-11-15 16:30 ` [ruby-core:71495] [Ruby trunk - Feature #11690] [Open] Update Hash during multiple assignment 6ftdan
2015-11-15 21:04   ` [ruby-core:71499] " Matthew Kerwin
2015-11-16 14:17 ` [ruby-core:71507] [Ruby trunk - Feature #11690] " 6ftdan
2015-11-16 21:31   ` [ruby-core:71508] " Matthew Kerwin
2015-11-17  1:29 ` [ruby-core:71509] " nobu
2015-11-17  2:10 ` [ruby-core:71510] " 6ftdan
2015-11-17  3:06 ` [ruby-core:71514] " nobu
2015-11-17 11:41 ` [ruby-core:71519] " 6ftdan
2015-11-18 18:41 ` [ruby-core:71562] " 6ftdan
2015-11-18 19:13 ` [ruby-core:71563] " 6ftdan
2015-11-22 23:29 ` [ruby-core:71635] " 6ftdan
2015-12-10 12:26 ` [ruby-core:72030] " 6ftdan

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).