ruby 变量类型
In this lesson, we are about to see different types of variables and constants. Although there are several types of variables like class variables and global variable, we will learn about local variables in this lesson. To create a variable in ruby, we simply provide a name and value using the assignment statement.
在本课程中,我们将看到变量和常量的不同类型。 尽管有几种类型的变量,例如类变量和全局变量,但我们将在本课程中学习局部变量。 要在ruby中创建变量,我们只需使用赋值语句提供名称和值。
number = 100
Ruby:变量命名规则 (Ruby: Rules for naming variables)
Must begin with lowercase letters or underscore like :
number
,_name
. Underscore come in handy when we create a compound variable using Snake Case like,必须以小写字母或下划线开头,例如:
number
,_name
。 当我们使用Snake Case创建复合变量时,下划线会派上用场,例如,
Camel Case.Camel Case 。first_name = "Bharath"
Another way to write compound variable or multiword variable islastName = "Kumar"
The Ruby Style Guide recommends consistent conventions and best practices to Ruby developers.
《 Ruby样式指南》向Ruby开发人员推荐了一致的约定和最佳实践。
Followed by the underscore or lowercase letter the next character of a variable can be a number or other letters, but not symbols.
下划线或小写字母后面的变量的下一个字符可以是数字或其他字母,但不能是符号。
_123, rate1 //valid #abc, n*ame //invalid variable names.
Names should match any of the reserved keywords. We'll look at the reserved keywords in ruby in the reserved keywords lesson.
Global variables are created by putting a dollar symbol
$
before the variable name. Ex.$salary = 10000
. salary is a global variable which can be seen throughout the program.全局变量是通过在变量名称前加上美元符号
$
来创建的。 例如$salary = 10000
。 薪水是一个全局变量,可以在整个计划中看到。
Ruby:数字数据类型 (Ruby: Number Datatype)
In this section, we are going to work with numbers data type. Ruby supports major number systems such as whole numbers, floating point numbers. Different bases such as Binary
, Octal
and Hexadecimal
are allowed.
在本节中,我们将使用数字数据类型。 Ruby支持主要的数字系统,例如整数,浮点数。 允许使用不同的基数,例如Binary
, Octal
和Hexadecimal
。

To write in binary we use 0b
and the corresponding number. For example, 5 can be written as 101 in binary. Likewise, to represent number in hexadecimal 0x
is used and for octal 0 alone is used. Negative numbers are represented by using unary operator - (minus) before the number.
要写二进制,我们使用0b
和相应的数字。 例如,5可以二进制形式写为101。 同样,以十六进制0x
表示数字,对于八进制0仅使用数字。 负数通过在数字前使用一元运算符- (减号)来表示。
Ruby supports different arithmetic operations such as addition, subtraction, multiplication, division, modulus and exponentiation.
Ruby支持不同的算术运算,例如加,减,乘,除,模和幂。

In above image, you can see the arithmetic operations performed.
在上图中,您可以看到执行的算术运算。
9 % 3
statement provides the remainder when 9 is divided by 3. In this case the result will be 0.9 % 3
语句提供9除以3时的余数。在这种情况下,结果将为0。2 ** 5
statement raises 2 to the power of 5. So, the result will be 2 * 2 * 2 * 2 * 2, which is equal to 32.2 ** 5
语句将2 ** 5
的幂。因此,结果将是2 * 2 * 2 * 2 * 2,等于32。
We will learn about operators in detail in a subsequent lesson.
我们将在随后的课程中详细了解操作员。
Ruby:ABS方法 (Ruby: ABS Method)
abs
is a method used to find the absolute value of a number. For example, -26.abs gives the value of 26. Some other methods are div and modulo.
abs
是一种用于查找数字绝对值的方法。 例如, -26.abs的值为26。其他一些方法是div和modulo 。

You can also convert numbers to strings. For example, to convert 100 into a string :
您也可以将数字转换为字符串。 例如,将100转换为字符串:
100.to_s
100.to_s

Ruby:布尔数据类型 (Ruby: Boolean Datatype)
Boolean type variables can hold two values true or false. But in ruby, it can also hold third type of value in Boolean variable called nil. But in most context, we work with only true and false values. So, let's concentrate on those two.
布尔型变量可以包含两个值true或false 。 但是在ruby中,它也可以在名为nil的布尔变量中保存第三种值。 但是在大多数情况下,我们只使用true和false值。 因此,让我们专注于这两个。
Boolean data type is used in comparison, loops and decision making statements. 100 > 1 returns true. Because 100 is greater than one. Likewise,1 > 100 returns false.
布尔数据类型用于比较,循环和决策语句。 100> 1返回true 。 因为100大于1。 同样, 1 > 100返回false。
You can also assign Boolean values to the variables.
您还可以将布尔值分配给变量。
flag = true
flag = true
After this assignment, the variable flag contains the Boolean value true.
分配后,变量标志包含布尔值true 。

Not operator (!)
negates the Boolean value. For example, if the flag
variable contains the value true. Then the negation of the flag variable gives the value of false.
不是运算符(!)
否定布尔值。 例如,如果flag
变量包含值true 。 然后flag变量的取反给出false的值。

You can also double negate the variable. But negating more than once tends to be confusing. Hence it's best to stick with single negate when you need it.
您还可以双重否定变量。 但是不止一次地否定往往会造成混乱。 因此,最好在需要时坚持使用单一求和法。
Ruby:常量 (Ruby: Constants)
Constants
are variables that holds the same value throughout the program. Ruby is a typical language, where you can reassign constants. As per conventions, all constants in ruby in uppercase characters so that they are easily distinguishable.
Constants
是在整个程序中具有相同值的变量。 Ruby是一种典型的语言,您可以在其中重新分配常量。 按照惯例,ruby中的所有常量均以大写字母表示,因此很容易区分。

Notice that it did change the value of the constant, but it gave a warning saying that "PI is already initialized constant".
请注意,它确实更改了常量的值,但发出警告说“ PI已初始化常量” 。
So, just be aware that you can define constants in ruby, you can also change its value which means you wonder they shouldn't be called constants
. As we already said, Ruby is a typical language.
因此,请注意,您可以在ruby中定义常量,也可以更改其值,这意味着您怀疑它们不应该被称为constants
。 如前所述,Ruby是一种典型的语言。
翻译自: https://www.studytonight.com/ruby/variables-in-ruby
ruby 变量类型