Project

General

Profile

ActionsLike0

Feature #11925

closed

Struct construction with kwargs

Added by prijutme4ty (Ilya Vorontsov) over 9 years ago. Updated over 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 - Feature #15076: Struct to raise error when keyword arguments used but not enabledRejectedActions
Related to Ruby - Feature #15222: Add a way to distinguish between Struct classes with and without keyword initializerOpenActions
Related to Ruby - Feature #16806: Struct#initialize accepts keyword arguments too by defaultClosedk0kubun (Takashi Kokubun)Actions
Has duplicate Ruby - Feature #9209: Struct instances creatable with named argsClosedActions
Has duplicate Ruby - Feature #13272: Keyword argument to instantiate a subclass of StructClosedActions

Added by k0kubun (Takashi Kokubun) over 7 years ago

Revision 02015974

struct.c: add keyword_init option to Struct.new

to initialize struct with keyword arguments.

[Feature #11925] [close GH-1771]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

ActionsLike0

Also available in: Atom PDF