• Source
    1. # define a class
    2. class Box
    3. # constructor method
    4. def initialize(w,h)
    5. @width, @height = w, h
    6. end
    7.  
    8. # accessor methods
    9. def getWidth
    10. @width
    11. end
    12. def getHeight
    13. @height
    14. end
    15.  
    16. # setter methods
    17. def setWidth=(value)
    18. @width = value
    19. end
    20. def setHeight=(value)
    21. @height = value
    22. end
    23. end
    24.  
    25. # create an object
    26. box = Box.new(10, 20)
    27.  
    28. # let us freez this object
    29. box.freeze
    30. if( box.frozen? )
    31. puts "Box object is frozen object"
    32. else
    33. puts "Box object is normal object"
    34. end
    35.  
    36. # now try using setter methods
    37. box.setWidth = 30
    38. box.setHeight = 50
    39.  
    40. # use accessor methods
    41. x = box.getWidth()
    42. y = box.getHeight()
    43.  
    44. puts "Width of the box is : #{x}"
    45. puts "Height of the box is : #{y}"