fork download
  1. class Dessert
  2. attr_accessor :name, :calories
  3. def initialize(name,calories)
  4. @name=name
  5. @calories=Float(calories)
  6. end
  7.  
  8. def healthy?
  9. @calories<200
  10. end
  11.  
  12. def delicious?
  13. true
  14. end
  15. end
  16.  
  17. class JellyBean < Dessert
  18. attr_accessor :flavor
  19. def initialize (name,calories,flavor)
  20. @flavor=flavor
  21. end
  22. def delicious?
  23. flavor=='black licorice'?false:true
  24. end
  25. def to_s
  26. "Name: #{@name}"
  27. end
  28. end
  29. icecream=Dessert.new('ice',100)
  30. p icecream
  31. p icecream.healthy?
  32. p icecream.delicious?
  33. ras=JellyBean.new('ice',100,'black licorice')
  34. p ras
  35. p ras.delicious?
  36. puts ras
Success #stdin #stdout 0.01s 7960KB
stdin
Standard input is empty
stdout
#<Dessert:0x0000563bec9f92e0 @name="ice", @calories=100.0>
true
true
#<JellyBean:0x0000563bec9f8f98 @flavor="black licorice">
false
Name: