Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. class Fruit
  2. attr_accessor :color, :type
  3. def initialize(color, type)
  4. @color=color ||= 'green'
  5. @type=type ||='pear'
  6. end
  7. end
  8.  
  9. apple=Fruit.new(red, apple)
  10.  
  11. class Fruit
  12. attr_accessor :color, :type
  13.  
  14. def initialize(params = {})
  15. @color = params.fetch(:color, 'green')
  16. @type = params.fetch(:type, 'pear')
  17. end
  18.  
  19. def to_s
  20. "#{color} #{type}"
  21. end
  22. end
  23.  
  24. puts(Fruit.new) # prints: green apple
  25. puts(Fruit.new(:color => 'red', :type => 'grape')) # prints: red grape
  26. puts(Fruit.new(:type => 'pomegranate')) # prints: green pomegranate
  27.  
  28. class Fruit
  29.  
  30. # Now this is the only thing you have to touch when adding defaults or properties
  31. def set_defaults
  32. @color ||= 'green'
  33. @type ||= 'pear'
  34. end
  35.  
  36. def initialize(params = {})
  37. params.each { |key,value| instance_variable_set("@#{key}", value) }
  38. set_defaults
  39. instance_variables.each {|var| self.class.send(:attr_accessor, var.to_s.delete('@'))}
  40. end
  41.  
  42. def to_s
  43. instance_variables.inject("") {|vars, var| vars += "#{var}: #{instance_variable_get(var)}; "}
  44. end
  45.  
  46. end
  47.  
  48. puts Fruit.new
  49. puts Fruit.new :color => 'red', :type => 'grape'
  50. puts Fruit.new :type => 'pomegranate'
  51. puts Fruit.new :cost => 20.21
  52. puts Fruit.new :foo => "bar"
  53.  
  54.  
  55. f = Fruit.new :potato => "salad"
  56. puts "f.cost.nil? #{f.cost.nil?}"
  57.  
  58. @color: green; @type: pear;
  59. @color: red; @type: grape;
  60. @color: green; @type: pomegranate;
  61. @color: green; @type: pear; @cost: 20.21;
  62. @color: green; @type: pear; @foo: bar;
  63. f.cost.nil? true
  64.  
  65. class Fruit
  66. attr_accessor :color, :type
  67.  
  68. def initialize(args={})
  69. options = {:color => 'green', :type => 'pear'}.merge(args)
  70.  
  71. self.color = options[:color]
  72. self.type = options[:type]
  73. end
  74. end
  75.  
  76. apple = Fruit.new(:color => 'red', :type => 'apple')
  77.  
  78. class Fruit
  79. attr_accessor :color, :type
  80. def initialize(color = 'green', type = 'pear')
  81. @color = color
  82. @type = type
  83. end
  84. def to_s
  85. "#{color} #{type}"
  86. end
  87. end
  88.  
  89.  
  90. puts Fruit.new # prints: green pear
  91. puts Fruit.new('red','apple') # prints: red apple
  92. puts Fruit.new(nil,'pomegranate') # prints: green pomegranate
  93.  
  94. class Fruit
  95. attr_accessor :color, :type
  96.  
  97. def initialize(args={})
  98. options = defaults.merge(args)
  99.  
  100. @color = options.fetch(:color)
  101. @type = options.fetch(:type)
  102. end
  103.  
  104. def defaults
  105. {
  106. color: 'green',
  107. type: 'pear'
  108. }
  109. end
  110. end
  111.  
  112. apple = Fruit.new(:color => 'red', :type => 'apple')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement