Created
August 27, 2012 03:10
-
-
Save rosylilly/3485237 to your computer and use it in GitHub Desktop.
Newtype on Ruby
This file contains 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
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