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

后端 未结 7 1321
遇见更好的自我
遇见更好的自我 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

    Brian's answer is excellent but I would like to suggest some modifications to make it mostly meta:

    class Fruit
    
      # Now this is the only thing you have to touch when adding defaults or properties
      def set_defaults
        @color ||= 'green'
        @type  ||= 'pear'
      end
    
      def initialize(params = {})
        params.each { |key,value| instance_variable_set("@#{key}", value) }
        set_defaults
        instance_variables.each {|var| self.class.send(:attr_accessor, var.to_s.delete('@'))}
      end
    
      def to_s
        instance_variables.inject("") {|vars, var| vars += "#{var}: #{instance_variable_get(var)}; "}
      end
    
    end
    
    puts Fruit.new
    puts Fruit.new :color => 'red', :type => 'grape'  
    puts Fruit.new :type => 'pomegranate'
    puts Fruit.new :cost => 20.21
    puts Fruit.new :foo => "bar"
    
    
    f = Fruit.new :potato => "salad"
    puts "f.cost.nil? #{f.cost.nil?}"
    

    Which outputs:

    @color: green; @type: pear; 
    @color: red; @type: grape; 
    @color: green; @type: pomegranate; 
    @color: green; @type: pear; @cost: 20.21; 
    @color: green; @type: pear; @foo: bar; 
    f.cost.nil? true
    

    Of course this wouldn't be a perfect solution for everything but it gives you some ideas on making your code more dynamic.

提交回复
热议问题