Last active
January 23, 2017 22:29
-
-
Save ixti/5cefaef2ca3b54dd869a34ebd9bc1a51 to your computer and use it in GitHub Desktop.
Human-friendly Struct :D
This file contains hidden or 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
| module HStruct | |
| def self.new(**props) | |
| attrs = props.map { |s| ":#{s}" }.join(", ") | |
| init = attrs.map { |s| "@#{s} = #{s}" }.join("\n") | |
| args = props.map { |s| "#{s}: nil" }.join(", ") | |
| Class.new do | |
| class_eval <<-RUBY | |
| attr_accessor #{attrs.map { |s| ":#{s}" }.join ", "} | |
| def initialize(#{args}) | |
| #{init} | |
| end | |
| RUBY | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment