What is the most efficient way to initialize a Class in Ruby with different parameters and default values?

后端 未结 7 1279
遇见更好的自我
遇见更好的自我 2020-12-23 11:59

I would like to have a class and some attributes which you can either set during initialization or use its default value.

class Fruit
  attr_accessor :color,         


        
相关标签:
7条回答
  • 2020-12-23 12:46

    Even more tasty syntactic sugar:

    class Fruit
      attr_accessor :color, :type
      def initialize *args
        @color, @type = args 
      end
    end
    
    pear = Fruit.new 'green', :pear
    
    0 讨论(0)
提交回复
热议问题