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
#2

Updated by naruse (Yui NARUSE) almost 9 years ago

  • Assignee deleted (core)
#8

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

  • Has duplicate Feature #9209: Struct instances creatable with named args added
#10

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

  • Has duplicate Feature #13272: Keyword argument to instantiate a subclass of Struct added
#16

Updated by k0kubun (Takashi Kokubun) over 7 years ago

  • Status changed from Open to Closed
#18

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Related to Feature #15076: Struct to raise error when keyword arguments used but not enabled added
#19

Updated by k0kubun (Takashi Kokubun) over 6 years ago

  • Related to Feature #15222: Add a way to distinguish between Struct classes with and without keyword initializer added
#20

Updated by k0kubun (Takashi Kokubun) almost 5 years ago

  • Related to Feature #16806: Struct#initialize accepts keyword arguments too by default added
ActionsLike0

Also available in: Atom PDF