Named Parameters in Ruby Structs

后端 未结 12 1718
小蘑菇
小蘑菇 2020-12-29 20:12

I\'m pretty new to Ruby so apologies if this is an obvious question.

I\'d like to use named parameters when instantiating a Struct, i.e. be able to specify which ite

12条回答
  •  没有蜡笔的小新
    2020-12-29 20:47

    Have you considered OpenStruct?

    require 'ostruct'
    
    person = OpenStruct.new(:name => "John", :age => 20)
    p person               # #
    p person.name          # "John"
    p person.adress        # nil
    

提交回复
热议问题