Issue #11925 has been updated by Yuki Kurihara.


Hi.

I have thought the same thing that want to use kwargs in Struct class.

But I can't come up with a good API.

Finally, I made a gem. https://github.com/ksss/type_struct

----------------------------------------
Feature #11925: Struct construction with kwargs
https://bugs.ruby-lang.org/issues/11925#change-55865

* Author: Ilya Vorontsov
* Status: Open
* Priority: Normal
* Assignee: ruby-core
----------------------------------------
Propose to make Struct subclass constructors which accept keyword arguments. Not sure, if it's reasonable to allow `.new` accept kwargs, so may be should use different method named like `.create`:

```ruby
  Point = Struct.new(:x,:y, :color)
  pt_1 = Point.create(x: 1, y: 2) # => Point<x: 1, y: 2, color: nil>
  pt_2 = Point.create!(x: 1, y: 2) # => ArgumentError, color not specified.
```

It will greatly simplify work with big structures, especially in cases when struct layout changes and for cases when structure can have lots of non-significant values. It also allows simpler ways to use implement default values for struct members.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>