Created
May 29, 2012 02:10
-
-
Save mungruby/2822135 to your computer and use it in GitHub Desktop.
Avoiding redefining constant Struct::ClassName warnings
This file contains 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
fields = [:switch, :sha_ind, :dtdm, :utdm, :ttdm, :actual] | |
def create_struct name, fields | |
Struct.new(name, *fields) | |
end | |
def create_singleton_struct name, fields | |
if Struct::const_defined? name | |
Struct.const_get name | |
else | |
Struct.new name, *fields | |
end | |
end | |
dto = create_struct 'HomingArrangement', fields | |
# warning: redefining constant Struct::HomingArrangement | |
dto = create_struct 'HomingArrangement', fields | |
dto = create_singleton_struct 'SwitchHomingArrangement', fields | |
# no warning | |
dto = create_singleton_struct 'SwitchHomingArrangement', fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, just what I needed !