Last active
December 12, 2022 07:13
-
-
Save sugamasao/3075bee962fea9bc479dd5b58477c4f3 to your computer and use it in GitHub Desktop.
Structに読み取り専用のmemberがほしい.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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