Skip to content

Instantly share code, notes, and snippets.

@willmcneilly
Created April 6, 2014 18:03
Show Gist options
  • Save willmcneilly/10009496 to your computer and use it in GitHub Desktop.
Save willmcneilly/10009496 to your computer and use it in GitHub Desktop.
## Example of an abstract class
## They should be treated as a protocol for their sub classes to implement
## They shouldn't be directly instantiated
class Session
def initialize(name, time)
@time = time
@name = name
end
def report
print "#{@time} minutes on #{@name} = #{calories} calories\n"
end
end
class RowingSession < Session
def calories
@time * 10
end
end
class ClimbingSession < Session
def initialize(name, time, program, weight)
super(name, time)
@program = program
@weight = weight
end
def calories
@time * 10
end
end
session_1 = RowingSession.new("Ronnie", 10)
session_1.report
session_2 = ClimbingSession.new("Billy", 5, "Hill Climbing", 70)
session_2.report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment