Skip to content

Instantly share code, notes, and snippets.

@xpepper
Last active November 4, 2017 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpepper/5367414 to your computer and use it in GitHub Desktop.
Save xpepper/5367414 to your computer and use it in GitHub Desktop.
My own (bare and simple) implementation of ruby's Struct core class
class MyStruct
#=> self is MyStruct
def self.new(*attributes)
#=> self is MyStruct
Class.new do
#=> self is the class instance
attr_accessor(*attributes)
end
end
end
Dave = MyStruct.new(:name, :surname)
Fred = MyStruct.new(:age)
dave = Dave.new
dave.name = "Dave"
puts dave.name #=> Dave
dave.surname = "Thomas"
puts dave.surname #=> Thomas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment