#! /usr/bin/ruby
# circle_area.rb computes the area of a circle given its radius
#
# Input: the radius of a circle
# Precondition: the radius is not negative
# Output: the area of the circle.
#
# Begun by: Prof. Adams, for CS 214 at Calvin College.
# Completed by:
# Date:
###############################################################
# function circleArea returns a circle's area, given its radius
# Parameters: r, a number
# Precondition: r > 0.
# Returns: the area of a circle whose radius is r.
PI = 3.1415927
def circleArea(r)
PI * r ** 2
end
if __FILE__ == $0
puts "To compute the area of a circle,"
print " enter its radius: "
radius = gets.chomp.to_f
print "The circle's area is: "
puts circleArea(radius)
end
As before, customize the program's opening documentation with
your name, the date, etc.
Unlike traditional compiled languages (which are translated into machine language), Ruby is an interpreted language, meaning we use an interpreter to run our program. From the command-line, enter:
ruby circle_area.rbThe Ruby interpreter (ruby) will check the syntax of your program and if there are no errors, will be running it.
When your program runs successfully, test its correctness by using the data values below.
1 2 2.5 4.99999Make certain your results are equivalent to those of our other languages before you continue.
In Ruby, comments begin with the # symbol and end at the end of the line. Go through the program line by line and add comments that explain what each line is doing.
What is the difference between Ruby's puts and print methods?
Then, use the script program to record what appears in your terminal:
Calvin > CS > 214 > Labs > 01 > Ruby