您的位置:首页 > 编程语言 > Ruby

ruby语法_Ruby语法

2020-08-03 01:35 1726 查看

ruby语法

Ruby语法 (Ruby syntax)

The syntax of Ruby is widely similar to that of Python and Perl. Keywords and braces are used to define the blocks for the code. We have got various keywords to define classes and objects. The syntax differs from Python and Perl in a way that all the instance variables are kept private and are only accessible through Access Modifiers.

Ruby的语法与Python和Perl的语法非常相似。 关键字和花括号用于定义代码块。 我们有各种关键字来定义类和对象。 语法与Python和Perl的不同之处在于,所有实例变量均保持私有状态,并且只能通过访问修饰符进行访问。

Let us go through the basic syntaxes:

让我们看一下基本语法:

变数 (Variables)

Variables are used to hold some data and reside at a specific memory location. Ruby supports five types of variables:

变量用于保存一些数据并驻留在特定的内存位置。 Ruby支持五种类型的变量:

i) Global variable

i)全局变量

You need to add $ at the beginning of the variable name if you want to make the variable global. By default, nil is assigned to the variable if you don't give a value to it explicitly.

如果要将变量设置为全局变量,则需要在变量名称的开头添加$ 。 默认情况下,如果未显式给变量赋值,则将nil分配给该变量。

Example:

例:

$global_variable = 90
[/code]

ii) Instance variable

ii)实例变量

If you want to create an Instance variable, add @ at the beginning of its name. Here also nil is assigned to it in case it is not initialized.

如果要创建实例变量,请在其名称的开头添加@ 。 在未初始化的情况下,也为它分配了nil 。

Example:

例:

@cust_id = 12
@student_name = "Hrithik"
[/code]

iii) Class variable

iii)类变量

Class variables begin with @@ and it is mandatory to initialize them.

类变量以@@开头,必须对其进行初始化。

Example:

例:

@@people_present = 0
[/code]

iv) Local variable

iv)局部变量

Local variables start with _ or a lowercase letter. The scope of a local variable starts from opening braces { and ends with the closing braces }.

局部变量以_或小写字母开头。 局部变量的范围从大括号{开始,到右括号}结束。

Example:

例:

myname = "Hrithik"
_siteaddress= "Includehelp.com"
[/code]

v) Constants

v)常数

They always begin with uppercase letters. You will have to face an error if you are referencing a constant which is not initialized.

它们始终以大写字母开头。 如果您引用的是未初始化的常量,则将不得不面对错误。

CONSTANT12 = 890
CONSTANTSTR = "INCLUDEHELP"
[/code]

弦乐 (Strings)

Strings are a sequence of characters. They can be declared in Ruby either by using single quotes ' ' or by using double quotes " ".

字符串是字符序列。 可以在Ruby中使用单引号''或双引号“”来声明它们。

"You are learning at Includehelp.com"
'This is also a string'
[/code]

数组 (Arrays)

An array can be declared in Ruby in the following way:

可以通过以下方式在Ruby中声明数组:

[1,'Shyam',5.34,'Include help']
[/code]

散列 (Hashes)

A hash in Ruby looks like the example which is given below,

Ruby中的哈希类似于下面给出的示例,

{name: "Harish"}
{:name=> "Satish"}
[/code]

Ruby代码的基本结构 (Basic structure of a Ruby code)

The basic structure of Ruby code is quite easy to understand. The method gets is used to input data from the user at the run time and the method puts is used for displaying the information to the user.

Ruby代码的基本结构非常容易理解。 方法gets用于在运行时从用户输入数据,而方法puts用于向用户显示信息。

Observe the code given below, you will find chomp, it is used for eliminating the new line character \n from the data which is entered.

观察下面给出的代码,您会发现chomp ,它用于从输入的数据中消除换行符\ n 。

You will also see that Type conversion is taking place. By default, the values are of string type and they are getting converted into integer type through the .to_i method.

您还将看到正在进行类型转换。 默认情况下,这些值是字符串类型,并且通过.to_i方法将它们转换为整数类型。

puts "Enter first value"
num=gets.chomp

puts "Enter second value"
num1=gets.chomp

pro=num.to_i*num1.to_i

puts "The product of #{num} and #{num1} is #{pro}"
[/code]

Output

输出量

Enter first value
12
Enter second value
34
The product of 12 and 34 is 408
[/code]

Ruby中的流控件 (Flow controls in Ruby)

Following are the flow control statements which are supported in Ruby programming language:

以下是Ruby编程语言支持的流控制语句:

  1. if statement

    如果声明

  2. if/else statement

    if / else语句

  3. elsif statement

    elsif声明

  4. unless statement

    除非声明

  5. switch statement

    切换语句

Let us study each of them with examples,

让我们通过示例研究它们,

a) if statement

a)如果声明

if statement only works for a single expression. It executes it and checks whether the statement stands to be true or false.

if语句仅适用于单个表达式。 它执行它并检查该语句是true还是false 。

Syntax:

句法:

if (condition)
#statement executes if the condition comes out to be True
end
[/code]

Example:

例:

a = 70
b = 9

if a>b
puts "a is greater"
end
[/code]

Output

输出量

a is greater
[/code]

(b) if-else statement

(b)if-else陈述

It is very similar to if statement, only it has an additional else with it. It works in the way that if the if condition is True then the code block inside it executes otherwise the code block inside else executes.

它与if语句非常相似,只是附加了其他语句。 它的工作方式是,如果if条件为True,则执行其中的代码块, 否则执行其中的代码块。

Syntax:

句法:

if(condition)
#code block
else
#code block
end
[/code]

Example:

例:

a = 70
b = 900

if a>b
puts "a is greater"
else
puts "b is greater"
end
[/code]

Output

输出量

b is greater
[/code]

(c) elsif statement

(c)elsif声明

If there are more than two options in your code, then elsif statement can be used. First the if condition is checked, if it comes to be false then the pointer moves to elsif condition, if it turns out to be true to the situation then elsif code block executes.

如果您的代码中有两个以上的选项,则可以使用elsif语句。 首先, 如果检查条件,如果它是假的,则指针移动到ELSIF条件,如果它原来是真实的,则情况ELSIF代码块执行。

Syntax:

句法:

if (condition)
#statements
elsif(condition)
#statements
else
#statements
end
[/code]

Example:

例:

a = 70
b = 900
c = 567

if (a>b and a>c)
puts "a is greater"
elsif (b>a and b>c)
puts "b is greater"
else
puts "c is greater"
end
[/code]

Output

输出量

b is greater
[/code]

(d) unless statement

(d)除非声明

unless statement is used to verify a statement false rather than it is true.

除非使用语句来验证语句为假而不是真实。

Syntax:

句法:

unless (condition)
#code block
else
#code block
end
[/code]

Example:

例:

Entry = false
unless Entry
puts "you have to wait"
else
puts "you are good to go!"
end
[/code]

Output

输出量

you have to wait
[/code]

(e) switch statement

(e)转换声明

The switch statement is used where you have multiple options. It is similar to elsif statement but it is more preferable than elsif. when keyword is used to check the conditions and whichever comes out to be true, the statements under it executes.

在有多个选项的地方使用switch语句。 它类似于elsif语句,但比elsif更可取。 当使用关键字检查条件时,无论哪个条件为真,都将执行其下的语句。

Syntax:

句法:

case (variable name)
when (condition)
#statements
when (condition)
#statements
else
#statements
end
[/code]

Example:

例:

puts "which fruit do you like?"
fruit = gets.chomp

case (fruit)
when 'apple'
puts 'Red colour'
when 'banana'
puts 'Yellow colour'
when 'papaya'
puts 'Green colour'
when 'kiwi'
puts 'Green colour'
else
puts 'bad choice'
end
[/code]

Output

输出量

Which fruit do you like?
banana
Yellow color
[/code]

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

ruby语法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: