Project

General

Profile

Actions

Feature #11925

closed

Struct construction with kwargs

Added by prijutme4ty (Ilya Vorontsov) almost 9 years ago. Updated almost 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:72595]

Description

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:

  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.


Related issues 5 (1 open4 closed)

Related to Ruby master - Feature #15076: Struct to raise error when keyword arguments used but not enabledRejectedActions
Related to Ruby master - Feature #15222: Add a way to distinguish between Struct classes with and without keyword initializerOpenActions
Related to Ruby master - Feature #16806: Struct#initialize accepts keyword arguments too by defaultClosedk0kubun (Takashi Kokubun)Actions
Has duplicate Ruby master - Feature #9209: Struct instances creatable with named argsClosedActions
Has duplicate Ruby master - Feature #13272: Keyword argument to instantiate a subclass of StructClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0