Ruby 实例变量
实例变量以 @ 开头。未初始化的实例变量的值为 nil,在使用 -w 选项后,会产生警告。
下面的实例显示了实例变量的用法。
#!/usr/bin/ruby
class Customer
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# 调用方法
cust1.display_details()
cust2.display_details()
在这里,@cust_id、@cust_name 和 @cust_addr 是实例变量。这将产生以下结果:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala