Created
November 8, 2011 06:22
-
-
Save pasberth/1347149 to your computer and use it in GitHub Desktop.
プロパティをセットしたりするモジュール
This file contains hidden or 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
| module Propertible | |
| alias propertible_original_method_missing method_missing | |
| def method_missing funcname, *args, &blk | |
| propertible_original_method_missing funcname, *args, &blk | |
| rescue => e | |
| if funcname.to_s =~ /^(.*)\=$/ | |
| self.class.send :attr_writer, $1 | |
| send :"#{$1}=", args[0] | |
| else | |
| self.class.send :attr_reader, funcname | |
| send funcname | |
| end | |
| end | |
| end | |
| class Person | |
| include Propertible | |
| def initialize name, age | |
| self.name = name | |
| self.age = age | |
| end | |
| end | |
| puts Person.new 'mana', 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment