$global_name = "Sir"
class Customer
@@no_of_customers = 0
def initialize(id, name, addr)
@cust_id = id
@cust_name = name
@cust_addr = addr
end
def display_details()
puts "#$global_name"
puts "Customer id is #@cust_id"
puts "Customer name is #@cust_name"
puts "Customer address is #@cust_addr"
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
cust1 = Customer.new('1', 'Tom', 'Downtown street')
cust2 = Customer.new('2', 'Jerry', 'Main avenue')
cust1.display_details()
cust1.total_no_of_customers()
全局變量:$開頭
實例變量:@開頭
類變量(在整個繼承鏈中都可訪問):@@開頭
def 定義一個方法,以end結束
單引號內僅表示字符串,雙引號內可以解析變量