Skip to content

Instantly share code, notes, and snippets.

@nakajima
Forked from NYiPhoneDeveloper/gist:110694
Created May 13, 2009 19:34
Show Gist options
  • Save nakajima/111235 to your computer and use it in GitHub Desktop.
Save nakajima/111235 to your computer and use it in GitHub Desktop.
Go through each of the commits here and see the process
of writing an object-oriented beginner Ruby program.
Good luck!
Pat
# A basic square program - will develop "inches", "ft", "yards" and add more features later!
class Square
# The #initialize method gets called whenever you instantiate
# a new instance of a class. So calling Square.new(6) will call
# this method, passing 6 as side_length.
def initialize(side_length)
@side_length = side_length
end
# This method could be replaced by using attr_reader:
#
# class Square
# # automatically generates the "getter" method
# attr_reader :side_length
#
# ...the rest of the code
#
# end
#
# Since we're just learning here, though, this is a good
# example of what a method does.
def side_length
@side_length
end
# Instance variables are accessible within instances of a class,
# so the side_length we set in #initialize can now be used here.
def perimeter
@side_length * 4
end
# Since we have a green test, we can feel comfortable refactoring
# our algorithm here...
def hypotenuse
Math.sqrt((@side_length * @side_length) * 2)
end
# Pretty easy right? Just like before. Write a failing test, then
# make it pass.
def area
@side_length * @side_length
end
end
# The __FILE__ variable is automatically set by Ruby to whatever file
# it's referenced in (gistfile1.rb in this case.)
#
# The $0 variable is set by Ruby to whatever the original file run by
# Ruby is. So if we run:
#
# $ ruby gistfile1.rb
#
# then the following code will be run. If we run:
#
# $ ruby gistfile1_spec.rb
#
# then the following code will not be run.
if __FILE__ == $0
# Now we can run our program, using the above class to handle the logic
# and the following code to prompt the user
puts "Enter the side length:"
# STDIN is the standard input IO object. Calling #gets actually ends up
# calling it there anyway, so let's make it explicit
side = STDIN.gets.to_f
# Create a new square, which has all the methods we defined above.
square = Square.new(side)
puts "What would you like?"
# This is sort of tricky, but it's easier than typing each thing out:
commands = {
'p' => :perimeter,
'h' => :hypotenuse,
'a' => :area
}
# First we loop over each command and tell its key
commands.each do |key, command|
puts "- to find the #{command}, enter: #{key}"
end
# Get the command key...
key = STDIN.gets.chomp
# Find which command it corresponds to...
command = commands[key]
# Finally, ask the square to tell us the result, using the
# send method, which is a way of calling methods on an object
# by simply passing the name:
result = square.send(command)
puts "Result: #{result}"
end
require 'rubygems'
require 'spec'
require 'gistfile1'
describe Square do
it "takes a side length" do
square = Square.new(6)
square.side_length.should == 6
end
describe "finding the perimeter" do
it "multiplies the sides by 4" do
square = Square.new(6)
square.perimeter.should == 24
end
end
describe "finding the hypotenuse" do
it "adds the squares of sides, then finds the square root" do
square = Square.new(6)
square.hypotenuse.should == Math.sqrt(6*6 + 6*6)
end
end
describe "finding the area" do
it "multiplies side lengths" do
square = Square.new(6)
square.area.should == 6*6
end
end
end
# A basic square program - will develop "inches", "ft", "yards" and add more features later!
class Square
# The math is meant for a square only!!
puts "put the side of the square: "
length = gets()
length = length.chomp.to_f
puts "Would you like: \
\np = perimeter \
\nh = hypotenuse \na = area"
ans = gets()
ans = ans.chomp.to_s
hy = Math.sqrt(length*length * 2)
if ans == "p"
puts length * 4
elsif ans == "a"
puts length * length
elsif ans == "h"
puts hy
else
puts "not a valid entery"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment