Common Ruby Idioms

前端 未结 15 2042
星月不相逢
星月不相逢 2020-12-07 07:13

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code)

However, inspired by this question: Ruby Code ex

15条回答
  •  感动是毒
    2020-12-07 07:29

    method missing magick

    class Dummy  
      def method_missing(m, *args, &block)  
        "You just called method with name #{m} and arguments- #{args}"  
      end  
    end
    
    Dummy.new.anything(10, 20)
    => "You just called method with name anything and arguments- [10, 20]"
    

    if you call methods that not exists in ruby objects, ruby interpreter will call method called 'method_missing' if its defined, you could user this for some tricks, like writing api wrappers, or dsl, where you don;t know all methods and parameters names

提交回复
热议问题