Objects
Goals
- Define a new object
- Create an instance of your object
- Call methods on your object
Step 1
Type this in the file circle.rb:class Circle def initialize(radius) @radius = radius end def area Math::PI * (@radius ** 2) end def perimeter 2 * Math::PI * @radius end end print "What is the radius of your circle? > " radius = gets.to_i a_circle = Circle.new(radius) puts "Your circle has an area of #{a_circle.area}" puts "Your circle has a perimeter of #{a_circle.perimeter}"Type this in the terminal:ruby circle.rb
Explanation
Back to Functions