Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
Created December 18, 2016 11:45
Show Gist options
  • Save osyo-manga/6fdacdd09c4dba8bc416d5fb524f9c19 to your computer and use it in GitHub Desktop.
Save osyo-manga/6fdacdd09c4dba8bc416d5fb524f9c19 to your computer and use it in GitHub Desktop.
require "stitcher"
using Stitcher
def type_struct **hash
Class.new {
stitcher_accessor hash
def initialize **hash
hash.each { |key, value| send("#{key}=", value) }
end
}
end
Human = type_struct(
name: String,
sex: String,
age: Integer,
address: String,
)
data = Human.new(
name: "michael",
sex: "man",
# sex: 114514, #TypeError
age: 48,
address: "California,USA",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment