Created
July 4, 2012 15:16
-
-
Save zdennis/3047852 to your computer and use it in GitHub Desktop.
coffee-script style constructor for ruby (for automatic instance variable setting)
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 initialize(*vars) | |
keys = vars.map{ |k| k.to_s.sub(/^@/, '')} | |
define_method :initialize do |args| | |
args.each_pair do |k,v| | |
raise ArgumentError, "Unexpected argument #{k}" unless keys.include?(k.to_s) | |
instance_variable_set "@#{k}", v | |
end | |
end | |
end | |
end | |
class Tile | |
initialize :@location, :@area | |
end | |
Tile.new location: "1,3", area: "24sqft." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about default values, and a block to allow you to write additional code in the initialize method?