[#118346] [Ruby master Bug#20586] Some filesystem calls in dir.c are missing error handling and can return incorrect results if interrupted — "ivoanjo (Ivo Anjo) via ruby-core" <ruby-core@...>
Issue #20586 has been reported by ivoanjo (Ivo Anjo).
13 messages
2024/06/19
[ruby-core:118275] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]
From:
"RubyBugs (A Nonymous) via ruby-core" <ruby-core@...>
Date:
2024-06-10 17:05:19 UTC
List:
ruby-core #118275
Issue #19001 has been updated by RubyBugs (A Nonymous).
Agreed, this need ended up being met by the initial implementation! Thank you, closing this ticket.
zverok (Victor Shepelev) wrote in #note-2:
> There isn't any need for this ticket as a separate request, as far as I am concerned.
> It works in the initial implementation of data already as submitted in https://bugs.ruby-lang.org/issues/16122#note-68, and even works with ol' good Struct, too:
> ```ruby
> Point = Struct.new(:x, :y, :z)
>
> Point[1, 0, 0].to_h.then { Point[**_1] } == Point[1, 0, 0] # => true
> ```
----------------------------------------
Feature #19001: Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]
https://bugs.ruby-lang.org/issues/19001#change-108768
* Author: RubyBugs (A Nonymous)
* Status: Open
----------------------------------------
*Extracted a follow-up to [#16122 Data: simple immutable value object](https://bugs.ruby-lang.org/issues/16122)*
# Proposal: Add a `#to_h` method symmetric to a constructor accepting keyword arguments
This allows round-trip between a `Hash` and a Value object instance, for example:
```ruby
Point = Data.define(:x, :y, :z)
points = [
Point.new(x: 1, y: 0, z: 0),
Point.new(x: 0, y: 1, z: 0),
Point.new(x: 0, y: 0, z: 1),
]
hashes = points.map(&:to_h)
points_2 = hashes.map { |h| Point.new(**h) }
points_2 == points
#=> true
```
## Why?
Having symmetric operation between `#to_h` and a keyword-args constructor is a major ergonomic factor in usage of immutable value objects.
To play with code that works like this, you may take a look at the [Values gem](https://rubygems.org/gems/values)
## Alternatives
If there is no symmetric construction and de-construction along these lines, a number of use cases become more complicated and less ergonomic.
--
https://bugs.ruby-lang.org/