掃碼下載編程獅APP
#!/usr/bin/ruby -w # 定義類 class Box # constructor method def initialize(w,h) @width, @height = w, h end # 實例方法 def getArea @width * @height end end # 創(chuàng)建對象 box = Box.new(10, 20) # 調(diào)用實例方法 a = box.getArea() puts "Area of the box is : #{a}"