Skip to content

Instantly share code, notes, and snippets.

@mungruby
Created May 29, 2012 02:10
Show Gist options
  • Save mungruby/2822135 to your computer and use it in GitHub Desktop.
Save mungruby/2822135 to your computer and use it in GitHub Desktop.
Avoiding redefining constant Struct::ClassName warnings
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
@gorille
Copy link

gorille commented Feb 17, 2015

Thanks, just what I needed !

@alejandrosobko
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment