Expand All

classes.step

goals do
  goal "Define a new object"
  goal "Create an instance of your object"
  goal "Call methods on your object"
end

step do
  message 'Create a new file called circle.rb'
  type_in_file 'circle.rb', <<-'CONTENTS'
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}"
  CONTENTS
  console 'ruby circle.rb'
  message 'When prompted, type in a radius for your circle.'
end

explanation do
  message "Functions by themselves aren't always enough to keep your program organized. **Object-oriented programming** was developed to keep related data (attributes) and functions that work on that data (methods) together."
  message "In Ruby, a new object is defined with the **class** keyword, followed by the name of your object (typically CamelCased). You finish the object definition later on with an **end**."
  message "Most objects define a special method, **initialize**, that saves the initial data your object is created with (here, a radius) and performs any other required set-up."
  message "You create an **instance** of your object with the **new** method. Arguments passed in to **new** are sent to your **initialize** method."
  message "Data is stored on your object using **instance variables** that start with an `@` sign. Instance variables behave like normal variables, but are only visible from inside a specific instance of your object. If you want the data to be externally accessible, you have to write more methods."
end

next_step 'how_to_write_a_program'