Skip to content

Instantly share code, notes, and snippets.

@YukiSakamoto
Created August 23, 2012 14:20
Show Gist options
  • Select an option

  • Save YukiSakamoto/3437089 to your computer and use it in GitHub Desktop.

Select an option

Save YukiSakamoto/3437089 to your computer and use it in GitHub Desktop.
ruby sample(method_missing and dispatch)
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