Skip to content

Instantly share code, notes, and snippets.

@dorkrawk
Created December 11, 2014 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dorkrawk/c86621e1f76209dc1f9e to your computer and use it in GitHub Desktop.
Save dorkrawk/c86621e1f76209dc1f9e to your computer and use it in GitHub Desktop.
A monkeypatch to Ruby Struct to get rid of "warning: redefining constant Struct::whatever" when using Structs
class Struct
def self.new_singleton(name, *fields)
if Struct::const_defined?(name)
Struct.const_get(name)
else
Struct.new(name, *fields)
end
end
end
@ckk-scratch
Copy link

So @dorkrawk - assuming I'm getting this in a rails project, do I put the singleton_struct.rb in my app/models?

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