ruby中变量_Ruby中的变量

ruby中变量

Ruby变量 (Ruby variables)

Ruby supports the following types of variables:

Ruby支持以下类型的变量

  1. Instance variable

    实例变量

  2. Class variable

    类变量

  3. Local variable

    局部变量

  4. Global variable

    全局变量

1)实例变量 (1) Instance variable)

An instance variable always starts with @. If you don't initialize an instance variable then it will get a nil value from compiler at the compile time. Their values are limited or local to certain instances of an object. There exist multiple copies of an instance variable and every object of the same class may have access to their local copy of instance variable. Any changes they made on the instance variable will not be reflected in the other's copy of that variable. Every copy of instance variable is independent of each other.

实例变量始终以@开头。 如果您不初始化实例变量,那么它将在编译时从编译器获取nil值。 它们的值对于对象的某些实例是有限的或局部的。 存在一个实例变量的多个副本,并且同一类的每个对象都可以访问其实例变量的本地副本。 他们对实例变量所做的任何更改都不会反映在该变量的其他副本中。 实例变量的每个副本彼此独立。

Syntax:

句法:

    @variable_name

Example:

例:

class Student
	def initialize(id, name, addr)
    	@stu_id = id
    	@stu_name = name
    	@stu_addr = addr
	end
	def display_details()
    	puts "Student id #@stu_id"
    	puts "Student name #@stu_name"
        puts "Student address #@stu_addr"
    end
end

stu1 = Student.new("1", "Karishma", "Raiwala")
stu2 = Student.new("2", "Kayra", "Bhopal")
stu1.display_details()
stu2.display_details()

Output

输出量

Student id 1
Student name Karishma
Student address Raiwala
Student id 2
Student name Kayra
Student address Bhopal

2)类变量 (2) Class variable)

A class variable is always declared with @@. If you don't initialize a class variable then you face an error at the compile time. Alike, Instance Variable, it is not implicitly initialized with nil. There is only one copy of a Class Variable and it is shared by the different objects of the same class. Any changes made by any object in the class variable will be reflected in every object that is accessing it. The class variable belongs to the whole class and is accessible anywhere inside the class.

类变量始终使用@@声明。 如果不初始化类变量,则在编译时会遇到错误。 与实例变量类似,它不会使用nil隐式初始化。 类变量只有一个副本,并且由同一类的不同对象共享。 类变量中任何对象所做的任何更改都会反映在访问它的每个对象中。 class变量属于整个类,可在该类内部的任何位置访问。

Syntax:

句法:

    @@variable_name

Example:

例:

class Student
	@@no_of_students = 0
	def initialize(id, name, addr)
		@stu_id = id
		@stu_name = name
		@stu_addr = addr
	end
	def display_details()
		puts "Student id #@stu_id"
		puts "Student name #@stu_name"
		puts "Student address #@stu_addr"
	end
	def total_no_of_students()
		@@no_of_students +=1
		puts "Total number of students are #@@no_of_students"
	end
end

stu1 = Student.new("1", "Karishma", "Raiwala")
stu2 = Student.new("2", "Kayra", "Bhopal")
stu1.display_details()
stu1.total_no_of_students()
stu2.display_details()
stu1.total_no_of_students()

Output

输出量

Student id 1
Student name Karishma
Student address Raiwala
Total number of students are 1
Student id 2
Student name Kayra
Student address Bhopal
Total number of students are 2


3)局部变量 (3) Local variable)

Local variables always start with '_' or lowercase letters (a-z). You will get an error by the compiler if you want to access the local variable outside the method in which it is declared. The scope of a local variable starts at def, do, or opening braces { and ends with the keyword end or with the closing braces }. There is no need to initialize a local variable. The lifetime of the local variable is decided or determined when the compiler compiles the code.

局部变量始终以'_'或小写字母( az )开头。 如果要在声明局部变量的方法之外访问局部变量,则编译器会出现错误。 局部变量的范围以def , do或开括号{结束,并以关键字end或闭括号}结尾。 无需初始化局部变量。 局部变量的生存期是在编译器编译代码时确定或确定的。

Syntax:

句法:

    _(variable_name)  or (variable_name) in lowercase

Example:

例:

class Student
	def update
		puts "Enter the number of students"
		no_of_students=gets.chomp
		puts "The updated numbers of students are #{no_of_students}"
	end
end
record1=Student.new
record1.update

Output

输出量

Enter the number of students
36
The updated numbers of students are 36

4)全局变量 (4) Global variable)

A global variable always starts with $. nil is provided to an uninitialized global variable and creates the warnings with –w option. The scope of a class variable is limited to the methods which are defined inside the class but if you want to declare a variable which is not limited only to a single class but can be accessed by multiple classes, in such case Global variable comes into the scenario. By default, an uninitialized global variable has a nil value. Global variables are least preferred or most of the times they are avoided because they make the program cryptic and complex.

全局变量始终以$开头。 将nil提供给未初始化的全局变量,并使用–w选项创建警告。 类变量的范围仅限于在类内部定义的方法,但是如果您要声明一个不仅限于单个类而且可以被多个类访问的变量,在这种情况下,全局变量会进入场景。 默认情况下,未初始化的全局变量的值为nil 。 最不希望使用全局变量,或者在大多数情况下避免使用全局变量,因为它们会使程序变得晦涩复杂。

Syntax:

句法:

    $(Variable_name)

Example:

例:

$global_variable = 10
class Class1 
	def print_global 
		puts "Global variable in Class1 is #$global_variable"
	end	
end

class Class2 
	def print_global 
		puts "Global variable in Class2 is #$global_variable"
	end
end

class1obj = Class1.new
class1obj.print_global 

class2obj = Class2.new
class2obj.print_global 

Output

输出量

Global variable in Class1 is 10
Global variable in Class2 is 10


翻译自: https://www.includehelp.com/ruby/variables.aspx

ruby中变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值