a simple demonstration of a class with two methods : Member method « Class « Ruby






a simple demonstration of a class with two methods



class Square
  def initialize(side_length)
    @side_length = side_length
  end

  def area
    @side_length * @side_length
  end
end

a = Square.new(10)
b = Square.new(5)
puts a.area
puts b.area

 








Related examples in the same category

1.Define a method to retrieve the value.
2.implement an each method, and get these methods for "free":
3.Compare method
4.Add method to a variable only
5.Defining Methods within a Class