Skip to content

Instantly share code, notes, and snippets.

@ixti
Last active January 23, 2017 22:29
Show Gist options
  • Select an option

  • Save ixti/5cefaef2ca3b54dd869a34ebd9bc1a51 to your computer and use it in GitHub Desktop.

Select an option

Save ixti/5cefaef2ca3b54dd869a34ebd9bc1a51 to your computer and use it in GitHub Desktop.
Human-friendly Struct :D
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