Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
Last active October 25, 2015 15:44
Show Gist options
  • Select an option

  • Save osyo-manga/6249c369fdde9884c494 to your computer and use it in GitHub Desktop.

Select an option

Save osyo-manga/6249c369fdde9884c494 to your computer and use it in GitHub Desktop.
ruby typed
def let **opt, &block
Object.new.instance_eval do
for name, type in opt
if type.class == Class
value = type.new
else
tyep = type.class
value = type
end
self.class.class_variable_set "@@#{name}", value
instance_eval{
name_ = name
define_singleton_method name do
self.class.class_variable_get "@@#{name_}"
end
type_ = type
define_singleton_method "#{name_}=" do |var|
raise "No match type." unless var.class <= type_
self.class.class_variable_set "@@#{name_}", var
end
}
end
instance_eval &block
end
end
let n: Numeric, str: String do
# ok
p n
p str
# error
self.n = "homu"
self.str = 42
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment