Skip to content

Instantly share code, notes, and snippets.

@sugamasao
Last active December 12, 2022 07:13
Show Gist options
  • Save sugamasao/3075bee962fea9bc479dd5b58477c4f3 to your computer and use it in GitHub Desktop.
Save sugamasao/3075bee962fea9bc479dd5b58477c4f3 to your computer and use it in GitHub Desktop.
Structに読み取り専用のmemberがほしい.rb
Person = Struct.new(:name, :age, :hoge, keyword_init: true) do
def initialize(**kw)
raise 'hoge は指定できへんで' if kw[:hoge]
super
self.hoge = :xxx
undef :hoge=
end
end
person = Person.new(name: 'foo', age: 10)
pp person.to_h # => {:name=>"foo", :age=>10, :hoge=>:xxx}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment