Skip to content

Instantly share code, notes, and snippets.

@pwightman
Created June 14, 2012 18:27
Show Gist options
  • Save pwightman/2931991 to your computer and use it in GitHub Desktop.
Save pwightman/2931991 to your computer and use it in GitHub Desktop.
class Dessert
attr_accessor :name, :calories
def initialize(name, calories)
@name = name
@calories = calories
end
def healthy?
@calories < 200
end
def delicious?
true
end
end
class JellyBean < Dessert
attr_accessor :flavor
def initialize(name, calories, flavor)
# super always calls the superclass's version of the method you're
# in. If you call super with no parameters, it will by default send
# all the parameters passed into your method. But since our method
# takes 3 parameters, and the superclass's only takes 2, we pass them
# in explicitly.
super name, calories
@flavor = flavor
end
def delicious?
@flavor != "black licorice"
end
end
jelly_bean = JellyBean.new("Foo", 250, "black licorice")
puts jelly_bean.name
puts jelly_bean.calories
puts jelly_bean.flavor
puts jelly_bean.delicious?
jelly_bean.flavor = "red"
puts jelly_bean.delicious?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment