Page 1 of 1

A Basic Understanding of Classes inside of Ruby

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 30 September 2009 - 08:45 PM

A Basic Understanding of Classes inside of Ruby:

In this tutorial I am going to show you how to create a class, write methods for a class, create an instance of a class, and then you will learn how to call methods from that class in multiple ways.

What are classes for?
Well, classes can be used to sort or organize your code. Usually when you implement a class, it is used for separating your code so it is easier to use. A class makes it easier to call the same methods over and over again. Instead of rewriting each method, you can write it once and just include your code for next time. For instance if I want to create a class to hold the methods to solve for the volume of six shapes, then I would create a class called 'VolumeOf'

class VolumeOf

end


Now that we have our class, we need to define some methods/functions. Since we want to get the volume of a cylinder, cube, sphere, pyramid, cone, and a rectangular prism; we will need to create 6 methods.

class VolumeOf
  
  def Cylinder
    
  end
  
  def Cube
    
  end
  
  def Sphere
  
  end
  
  def Pyramid
    
  end
  
  def Cone
  
  end
  
  def RectangularPrism
    
  end
  
end


Since the volume formulas require some user input we need to add some parameters. We can't pass any data into our methods yet because we don't have any parameters. We are going to add the 'radius' and 'height' as a parameter for the Cylinder, the 'side' as a parameter for the Cube, the 'radius' for the Sphere, the 'base' and 'height' for the Pyramid, the 'base', 'height', and 'radius' for the Cone, and the 'depth', 'height', and 'width' for the RectangularPrism. Remember when adding parameter(s) your parameter(s) need to be within a '(' and ')'. If there is more than one parameter then you need to use a comma ',' to separate them. This is what your code should look like:
class VolumeOf
  
  def Cylinder(radius, height)
    
  end
  
  def Cube(side)
    
  end
  
  def Sphere(radius)
  
  end
  
  def Pyramid(base, height)
    
  end
  
  def Cone(base, height, radius)
  
  end
  
  def RectangularPrism(depth, height, width)
    
  end
  
end


Now that we have our parameters in place, we need to actually code the methods. When finding the volume of a cylinder we need the radius, the height, and an approximate for pi; we will be using 3.14159 as our approximate for pi. The formula for finding the volume of a cylinder is pi * radius[squared] * height. In ruby the way we get the square of the radius is: radius * radius or we can use radius ** 2; either way will work. In this case we are going to use radius ** 2. After we get the volume we need to return the volume. Your cylinder code should look something like this:
# Gets the volume of a Cylinder
def Cylinder(radius, height)
  # Gets an approximate for pi
  pi = 3.14159
  # Gets the volume
  volume = pi * (radius ** 2) * height
  # Returns the volume
  return volume
  # Exit the mehtod
end


We are going to do the same thing, but this time we will be using different shapes, so they will require different formulas to find the volume. The volume of a cube is side[cubed] or side * side * side. This is probably the easiest one to find the volume of. All you need to do is know the measure of one side.
# Gets the volume of a Cube
def Cube(side)
  # Gets the cube of the side
  volume = (side ** 3)
  # Returns the volume
  return volume
  # Exit the mehtod
end


The volume of a sphere is (4/3, or 1 1/3) multiplied by an approximate for pi, and we have to cube the radius. So it looks like this when we put it into code:
# Gets the volume of a Sphere
def Sphere(radius)
  # Gets the fraction 4/3 or 1 1/3
  fraction = (4 / 3)
  # Gets an approximate for pi
  pi = 3.14159
  # Gets the volume
  volume = fraction * pi * (radius ** 3)
  # Return the volume
  return volume
  # Exit the method
end


With a pyramid we need a fraction(1/3), the pyramid's base and pyramid's height. To get the volume all we do is multiply the fraction(1/3) by the base, then multiply that by the height. Then don't forget to return the volume.
# Gets the volume of a Pyramid
def Pyramid(base, height)
  # Gets the fraction 1/3
  fraction = (1 / 3)
  # Gets the volume
  volume = fraction * base * height
  # Return the volume
  return volume
  # Exit the method
end


Getting the volume of a cone is similar to getting the volume of a pyramid except for the parameter we need include parameter for the radius as well as the base and height. The formula to find the volume of the cone is: (1/3) multiplied by an approximate for pi multiplied by the radius[squared] multiplied by the height. Once again don't forget that we are using 3.14159 as an approximate for pi.
# Gets the volume of a Cone
def Cone(base, height, radius)
  # Gets the fraction 1/3
  fraction = (1 / 3)
  # Gets an approximate for pi
  pi = 3.14159
  # Gets the volume
  volume = fraction * pi * (radius ** 2) * height
  # Return the volume
  return volume
  # Exit the method
end


Ok last one. We are going to get the volume of a rectangular prism. For this one all you need to do is get the depth, the height, and the width; then multiply them all together.
# Gets the volume of a Rectangular Prism
def RectangularPrism(depth, height, width)
  # Getst the volume
  volume = depth * height * width
  # Return the volume
  return volume
  # Exit the mehtod
end


Ok here is the full code in case you got lost somewhere:
# Gets the volumes of a Cylinder, Cube, Sphere, Pyramid, Cone, and Rectangular Prism.
class VolumeOf
  
  # Gets the volume of a Cylinder
  def Cylinder(radius, height)
    # Gets an approximate for pi
    pi = 3.14159
    # Gets the volume
    volume = pi * (radius ** 2) * height
    # Returns the volume
    return volume
    # Exit the mehtod
  end
  
  # Gets the volume of a Cube
  def Cube(side)
    # Gets the cube of the side
    volume = (side ** 3)
    # Returns the volume
    return volume
    # Exit the mehtod
  end
  
  # Gets the volume of a Sphere
  def Sphere(radius)
    # Gets the fraction 4/3 or 1 1/3
    fraction = (4 / 3)
    # Gets an approximate for pi
    pi = 3.14159
    # Gets the volume
    volume = fraction * pi * (radius ** 3)
    # Return the volume
    return volume
    # Exit the method
  end
  
  # Gets the volume of a Pyramid
  def Pyramid(base, height)
    # Gets the fraction 1/3
    fraction = (1 / 3)
    # Gets the volume
    volume = fraction * base * height
    # Return the volume
    return volume
    # Exit the method
  end
  
  # Gets the volume of a Cone
  def Cone(base, height, radius)
    # Gets the fraction 1/3
    fraction = (1 / 3)
    # Gets an approximate for pi
    pi = 3.14159
    # Gets the volume
    volume = fraction * pi * (radius ** 2) * height
    # Return the volume
    return volume
    # Exit the method
  end
  
  # Gets the volume of a Rectangular Prism
  def RectangularPrism(depth, height, width)
    # Getst the volume
    volume = depth * height * width
    # Return the volume
    return volume
    # Exit the mehtod
  end
  
  # Exit the class
end


Ok now that we have finished coding all of the methods we are going to learn how to create a new instance of a class, and then call the methods multiple ways.

The first way I will show you will be when you only need to call a method once from a class. By that I mean, if you need to call a method more than once do NOT use Methods 1 & 2, skip ahead to Method 3.
Method 1:
# Creates a new instance of the 'VolumeOf' class
# and gets the volume of a Cylinder
VolumeOf.new.Cylinder(5,23)


Method 2:
# Creates a new instance of the 'VolumeOf' class
# and gets the volume of a Cylinder
(VolumeOf.new).Cylinder(5,23)


Method 3:
# Creates a new instance of the 'VolumeOf' class
volumes = VolumeOf.new
# Gets the volume of a Cylinder
volumes.Cylinder(5,23)


Remember that if you want to call a method more than once, then I recommend that you use Method 3, otherwise you will have to keep defining new instances of the VolumeOf class. Ok now you should have a Basic Understanding of Classes inside of Ruby.

Is This A Good Question/Topic? 2
  • +

Replies To: A Basic Understanding of Classes inside of Ruby

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Posted 07 October 2009 - 11:39 PM

It is more likely that each of those methods would actually be classes themselves, and each one of those classes would inherit from a basic Shape class which would define a volume_of method that the child classes would redefine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1