Created
August 23, 2012 14:20
-
-
Save YukiSakamoto/3437089 to your computer and use it in GitHub Desktop.
ruby sample(method_missing and dispatch)
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 SampleClass | |
| def send(method_name, *args) | |
| puts "#{method_name}, #{args}\n" | |
| end | |
| def method_missing(method_name, *args) | |
| if method_name.to_s.match(/get_(.*)?_value/) | |
| puts "Getter: #{$1}" | |
| elsif method_name.to_s.match(/set_(.*?)_value/) | |
| puts "Setter: #{$1}" | |
| else | |
| puts "Missing!: #{method_name}" | |
| end | |
| end | |
| def initialize | |
| end | |
| end | |
| if __FILE__ == $0 | |
| sc = SampleClass.new | |
| sc.send(:Hogehoge, 1,2,3,4) | |
| sc.set_aaaa_value(4) | |
| sc.get_abcd_value(3) | |
| sc.foobar() | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment