Ruby - 基本语法

Ruby - 基本语法 首页 / Ruby入门教程 / Ruby - 基本语法

让无涯教程用Ruby编写一个简单的程序,所有的ruby文件都将具有扩展名 .rb 。因此,将以下源代码放入test.rb文件中。

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

在这里,假设您在/usr/bin目录中有Ruby解释器,现在,尝试按以下方式运行此程序-

$ruby test.rb

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-syntax.html

来源:LearnFk无涯教程网

Hello, Ruby!

Ruby 空格

空格和制表符之类的空格字符通常在Ruby代码中会被忽略,除非它们出现在字符串中。启用-w选项时,此类解释会产生警告。

a + b is interpreted as a+b ( Here a is a local variable)
a +b is interpreted as a(+b) ( Here a is a method call)

Ruby 标识符

标识符是变量,常量和方法的名称。 Ruby标识符区分大小写。这意味着RamRAM是Ruby中的两个不同的标识符。

Ruby标识符名称可以包含字母数字字符和下划线字符(_)。

Ruby 保留字

以下列表显示了Ruby中的保留字。这些保留字不能用作常量或变量名。但是,它们可以用作方法名称。

BEGINdonextthen
END else nil true
alias elsifnot undef
and endorunless
beginensure redountil
break falserescuewhen
caseforretrywhile
class ifreturnwhile
def inself __ FILE __
definedmodule super __ LINE __

Ruby 多行文档

"Here Document" 是指从多行构建字符串。 在<<之后,您可以指定字符串或标识符以终止字符串文字,并且当前行之后直至终止符的所有行都是字符串的值。

如果用引号引起来,则引号的类型确定面向行的字符串文字的类型。 注意,<<和终止符之间不能有空格。

#!/usr/bin/ruby -w

print <<EOF
   This is the first way of creating
   here document ie. multiple line string.
EOF

print <<"EOF";                # same as above
   This is the second way of creating
   here document ie. multiple line string.
EOF

print <<`EOC`                 # execute commands
	echo hi there
	echo lo there
EOC

print <<"foo", <<"bar"  # you can stack them
	I said foo.
foo
	I said bar.
bar

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-syntax.html

来源:LearnFk无涯教程网

   This is the first way of creating
   her document ie. multiple line string.
   This is the second way of creating
   her document ie. multiple line string.
hi there
lo there
      I said foo.
      I said bar.

Ruby BEGIN 语句

BEGIN {
   code
}

声明代码在程序运行之前被调用。

#!/usr/bin/ruby

puts "This is main Ruby Program"

BEGIN {
   puts "Initializing Ruby Program"
}

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-syntax.html

来源:LearnFk无涯教程网

Initializing Ruby Program
This is main Ruby Program

Ruby END 语句

END {
   code
}

声明要在程序结尾处调用的 code 。

无涯教程网

#!/usr/bin/ruby

puts "This is main Ruby Program"

END {
   puts "Terminating Ruby Program"
}
BEGIN {
   puts "Initializing Ruby Program"
}

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-syntax.html

来源:LearnFk无涯教程网

Initializing Ruby Program
This is main Ruby Program
Terminating Ruby Program

Ruby 注释

注释在Ruby解释器中隐藏了一行,一行的一部分或几行。您可以在行的开头使用井号(#)-

# I am a comment. Just ignore me.

或者,注释可以在语句或表达式后的同一行上-

name="Madisetti" # This is again comment

您可以注释多行,如下所示:

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

这是另一种形式。该块注释使用= begin ... =end从解释器中隐藏了几行-

=begin
This is a comment.
This is a comment, too.
This is a comment, too.
I said that already.
=end

Ruby 常量

第一个大写字母的标识符是Ruby中的常量。在编程中,习惯上以大写形式写入常量的所有字符。

#!/usr/bin/ruby

Name = "Robert"
AGE = 23

Name = "Juliet"

在上面的示例中,创建了两个常量。常量之一将在以后重新定义。

Name = "Robert"
AGE = 23

创建两个常量。当标识符的名称以大写字母开头时,在Ruby中将有一个常量。按照约定,常量通常用大写字母表示。

Name = "Juliet"

无涯教程重新定义一个常数。哪个发出警告。

$ ./constants.rb 
./constants.rb:6: warning: already initialized constant Name
./constants.rb:3: warning: previous definition of Name was here

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

从0开始学架构 -〔李运华〕

Nginx核心知识150讲 -〔陶辉〕

Kafka核心技术与实战 -〔胡夕〕

说透中台 -〔王健〕

爱上跑步 -〔钱亮〕

跟着高手学复盘 -〔张鹏〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)