ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91028] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
@ 2019-01-11 19:45 ` alissonbruno.sa
  2019-01-11 19:57 ` [ruby-core:91030] " zverok.offline
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: alissonbruno.sa @ 2019-01-11 19:45 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been reported by alissonbruno.sa (Alisson  Santos).

----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

* [ruby-core:91030] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
  2019-01-11 19:45 ` [ruby-core:91028] [Ruby trunk Feature#15526] New way to destruct an object hash alissonbruno.sa
@ 2019-01-11 19:57 ` zverok.offline
  2019-01-11 20:15 ` [ruby-core:91031] " shevegen
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: zverok.offline @ 2019-01-11 19:57 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been updated by zverok (Victor Shepelev).


Not exactly what you are describing, but, funny enough, there is a way!

```ruby
config = { host: 'localhost', port: 3000 }

config.then { |host:, port:|
  p "host=#{host}, port=#{port}"
}
```

----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526#change-76251

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

* [ruby-core:91031] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
  2019-01-11 19:45 ` [ruby-core:91028] [Ruby trunk Feature#15526] New way to destruct an object hash alissonbruno.sa
  2019-01-11 19:57 ` [ruby-core:91030] " zverok.offline
@ 2019-01-11 20:15 ` shevegen
  2019-01-11 20:35 ` [ruby-core:91032] " zverok.offline
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: shevegen @ 2019-01-11 20:15 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been updated by shevegen (Robert A. Heiler).


> What do you guys think?

Ultimately you only have to convince matz, so the rest is just people giving opinions. :)

> Not exactly what you are describing, but, funny enough, there is a way!

    config.then { |host:, port:|

Admit it, you only wanted to use **.then**. ;)

To the original suggestion:

    { host, port } = config

Personally I am not a huge fan of the syntax proposal simply due to
the {} part. My brain tends to associate this as a Hash, so I get confused
when the {} is on the left side. {} already has quite some meanings, e. g.
do/end. I would rather prefer to keep any meaning of {} smaller rather
than expand on it.

Syntax aside, I am not sure I like the proposal as such either, but I don't
care that much really. My opinion is slightly against it but it's not that
strong.

> I know Ruby has Hash#values_at, but I think this way it's more readable
> and understandable

I prefer .values_at since I like object.method notation in general, unless
there is a significantly shorter and readable way that does not cause
that much confusion. But I think this is difficult to agree because what 
may be readable or easy to understand for one person, may be difficult
to understand for someone else.

Actually, although I myself still am not using yield_self/then, I'd rather
prefer the variant shown by Victor, rather than the { } variant on the 
left hand side, but that may be just my own personal opinion.

If you feel strongly about your proposal you could consider adding your
proposal to any upcoming developer meeting.


----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526#change-76252

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

* [ruby-core:91032] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-01-11 20:15 ` [ruby-core:91031] " shevegen
@ 2019-01-11 20:35 ` zverok.offline
  2019-01-12 15:05 ` [ruby-core:91047] " janfri26
  2019-01-14 15:45 ` [ruby-core:91083] " elia
  5 siblings, 0 replies; 6+ messages in thread
From: zverok.offline @ 2019-01-11 20:35 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been updated by zverok (Victor Shepelev).


> Admit it, you only wanted to use `.then`

That's absolutely not the point. The real point here is: 

1) Ruby has very consistent "argument **deconstruction**" rules. Basically, "it is deconstructed the same way it is constructed", e.g., if you can _call_ (construct) with arguments like `meth(a, *b, c: 1, d: 2, **e)`, you can also _receive_ (deconstruct) arguments exactly the same way: `a, *b, c:, d: nil, **e`.

2) The deconstruction is fully enabled in arguments (including keyword arguments) to procs and methods

3) Initially (before keyword arguments), the variable assignment had the same "expressive power", e.g. you could define a method like 

```ruby
def meth(a, b, *rest)
```
...and you could assign variables the same way:

```ruby
a, b, *rest = [1, 2, 3, 4]
```

4) When keyword arguments were introduced, there was no matching syntax for variable assignment. If it **would** exist, it most probably **should** look this way (same as arguments, remember):

```ruby
a:, b:, **kwrest = {a: 1, b: 2, c: 3, d: 4}
```
It obviously looks confusing (and would probably be hard for the parser), and I believe that's the reason it was NOT added to the language.

5) I don't think adding "orphan feature" (looking like nothing else in the language) just for the sake of this particular case would be ever accepted; and I don't think it should.

6) In the meantime, "chainable" code style (with `Enumerable` and `#then`) allows to embrace argument deconstruction in its full force:
```ruby
config.then { |host:, port: 8080, **rest|
```
...which is NOT the main (and obviously not the only) reason to use this style, just a nice side-effect of its consistency with the rest of the language.

----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526#change-76253

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

* [ruby-core:91047] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-01-11 20:35 ` [ruby-core:91032] " zverok.offline
@ 2019-01-12 15:05 ` janfri26
  2019-01-14 15:45 ` [ruby-core:91083] " elia
  5 siblings, 0 replies; 6+ messages in thread
From: janfri26 @ 2019-01-12 15:05 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been updated by janfri (Jan Friedrich).


zverok (Victor Shepelev) wrote:
> 1) Ruby has very consistent "argument **deconstruction**" rules. Basically, "it is deconstructed the same way it is constructed", e.g., if you can _call_ (construct) with arguments like `meth(a, *b, c: 1, d: 2, **e)`, you can also _receive_ (deconstruct) arguments exactly the same way: `a, *b, c:, d: nil, **e`.
> 
> 2) The deconstruction is fully enabled in arguments (including keyword arguments) to procs and methods
> 
> 3) Initially (before keyword arguments), the variable assignment had the same "expressive power", e.g. you could define a method like 
> 
> ```ruby
> def meth(a, b, *rest)
> ```
> ...and you could assign variables the same way:
> 
> ```ruby
> a, b, *rest = [1, 2, 3, 4]
> ```
> 
> 4) When keyword arguments were introduced, there was no matching syntax for variable assignment. If it **would** exist, it most probably **should** look this way (same as arguments, remember):
> 
> ```ruby
> a:, b:, **kwrest = {a: 1, b: 2, c: 3, d: 4}
> ```
> It obviously looks confusing (and would probably be hard for the parser), and I believe that's the reason it was NOT added to the language.
> 
> 5) I don't think adding "orphan feature" (looking like nothing else in the language) just for the sake of this particular case would be ever accepted; and I don't think it should.
> 
> 6) In the meantime, "chainable" code style (with `Enumerable` and `#then`) allows to embrace argument deconstruction in its full force:
> ```ruby
> config.then { |host:, port: 8080, **rest|
> ```
> ...which is NOT the main (and obviously not the only) reason to use this style, just a nice side-effect of its consistency with the rest of the language.

+1 Great analysis.


----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526#change-76267

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

* [ruby-core:91083] [Ruby trunk Feature#15526] New way to destruct an object hash
       [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-01-12 15:05 ` [ruby-core:91047] " janfri26
@ 2019-01-14 15:45 ` elia
  5 siblings, 0 replies; 6+ messages in thread
From: elia @ 2019-01-14 15:45 UTC (permalink / raw)
  To: ruby-core

Issue #15526 has been updated by elia (Elia Schito).


Related/duplicate https://bugs.ruby-lang.org/issues/11758

----------------------------------------
Feature #15526: New way to destruct an object hash
https://bugs.ruby-lang.org/issues/15526#change-76311

* Author: alissonbruno.sa (Alisson  Santos)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
JavaScript has a nice a neat way to destruct objects.

~~~ javascript
const person = { name: "John Doe", age: 33 };
const { name, age } =  person;
~~~

Erlang has a similar way to destruct a tuple:
~~~ erlang
Person = {"John Doe", 33}
{Name, Age} = Person
~~~

I think it's very handy and would be nice if we have something similar in Ruby.

~~~ ruby
config = { host: 'localhost', port: 3000 } 
{ host, port } = config
~~~

I know Ruby has Hash#values_at, but I think this way it's more readable and understandable

What do you guys think?




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

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

end of thread, other threads:[~2019-01-14 15:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15526.20190111194516@ruby-lang.org>
2019-01-11 19:45 ` [ruby-core:91028] [Ruby trunk Feature#15526] New way to destruct an object hash alissonbruno.sa
2019-01-11 19:57 ` [ruby-core:91030] " zverok.offline
2019-01-11 20:15 ` [ruby-core:91031] " shevegen
2019-01-11 20:35 ` [ruby-core:91032] " zverok.offline
2019-01-12 15:05 ` [ruby-core:91047] " janfri26
2019-01-14 15:45 ` [ruby-core:91083] " elia

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