Skip to content

Instantly share code, notes, and snippets.

@rosylilly
Created August 27, 2012 03:10
Show Gist options
  • Save rosylilly/3485237 to your computer and use it in GitHub Desktop.
Save rosylilly/3485237 to your computer and use it in GitHub Desktop.
Newtype on Ruby
class Class
def new_type(&block)
new_class = self.dup
new_class.class_eval(&block) if block_given?
new_class
end
end
CArray = Array.new_type do
def example
sample
end
end
cary = CArray.new(10){ rand(10) }
p cary.example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment