Last active
August 29, 2015 14:12
-
-
Save zvlex/ac142caa4716d124eb8e to your computer and use it in GitHub Desktop.
my Hashie Mash basic implementation
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
| class Mash | |
| def initialize | |
| @mash = {} | |
| end | |
| def method_missing(method_id, *args) | |
| string_method = method_id.to_s | |
| if is_predicate?(method_id) | |
| key = string_method[0...-1] | |
| @mash.include?(key) | |
| else | |
| if args.empty? | |
| @mash[string_method] | |
| else | |
| key = string_method[0...-1] | |
| @mash[key] = args.join | |
| @mash[key] | |
| end | |
| end | |
| end | |
| def is_predicate?(method) | |
| return true if method =~ /[a-zA-z]\?\z/ | |
| false | |
| end | |
| end | |
| mash = Mash.new | |
| p mash.hello? | |
| mash.hello = "let me say" | |
| p mash.hello | |
| p mash.hello? | |
| p mash.alex? | |
| mash.alex = "Ruby Programming Language" | |
| p mash.alex? | |
| p mash.alex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment