Ruby OOP correct concept? -
the exercise questions below answers.
#create tree class rings attribute , getter method. #trees create ring every winter passes #it should have bear_fruit? method should return true if #has fruit year. tree produces fruit when has #more 7 rings less 15, false otherwise. #the class should have winter_season method increases #rings attr 1.
can give me constructive criticism on code?
class tree attr_accessor :winters, :rings, :bear_fruit? def initialize(winters, rings) @winters = winters @rings = rings end def rings_created @winters = 0 @rings = 0 while @winters == @rings @winters +=1 @rings +=1 break if @winters == 100 end end end def bear_fruit if @rings > 6 || < 16 @bear_fruit? = true else @bear_fruit? = false end end def winter_season @winters = 0 @rings = 0 while @winters < @rings @winters +=1 @rings +=2 break if @winters == 100 end end end end
according exercise, supposed create class tree
single attribute rings
, 2 methods, bear_fruit?
, winter_season
:
- create
tree
class
- a
rings
attribute , getter method- a
bear_fruit?
method
- returns
true
if tree has more 7 rings less 15- returns
false
otherwise- a
winter_season
method
- increases
rings
1
that's all. doesn't tree should track winters , doesn't mention loops.
here's how implement it:
class tree attr_reader :rings def initialize @rings = 0 end def bear_fruit? @rings > 7 && @rings < 15 end def winter_season @rings += 1 end end
Comments
Post a Comment