Last active
November 4, 2017 10:27
-
-
Save xpepper/5367414 to your computer and use it in GitHub Desktop.
My own (bare and simple) implementation of ruby's Struct core class
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
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