类、变量、方法

$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结束

 

单引号内仅表示字符串,双引号内可以解析变量

你可能感兴趣的:(ruby)