ruby执行shell命令
It is also called irb. It allows us to type ruby expressions or statements. Open the command prompt and type irb. (both Windows and Macintosh).
也称为irb。 它允许我们键入ruby表达式或语句。 打开命令提示符,然后键入irb。 (Windows和Macintosh)。

As you can see in the above screen, we can write ruby expressions and statements. The texts entered within quotes are called String. We can perform Concatenation Operation (concatenation - Joining two Strings) on two strings.
从上面的屏幕中可以看到,我们可以编写ruby表达式和语句。 用引号引起来的文本称为String 。 我们可以对两个字符串执行串联操作(串联-连接两个字符串)。
We can store the value to the variable simply like,
我们可以将值存储到变量中,就像
name = “Welcome to Ruby!”
Ruby is a case sensitive programming language. So, name is different from Name and NAME.
Ruby是区分大小写的编程语言。 因此, 名称不同于Name和NAME 。
We can also perform some complicated operations. See the below code snippet which prints numbers from 1 to 9.
我们还可以执行一些复杂的操作。 请参见下面的代码段,该代码段打印从1到9的数字。
i = 1
while i < 10
print i
end

Output :
输出:

Whoa!! What just happened? You might be confused after seeing the output. We said it prints 1 to 9. But it's showing all 1's and that also too many. This is because the variable i
was not incremented. Press Ctrl + C to terminate the execution.
哇! 刚才发生了什么? 看到输出后,您可能会感到困惑。 我们说它打印1到9。但是它显示所有1,并且也太多。 这是因为变量i
没有增加。 按Ctrl + C终止执行。
The following code prints from 1 to 9
以下代码从1到9打印
i = 1
while i < 10
print i
i += 1
end
We can also define functions in irb
. A function is a set of statement(s) to perform a particular task. Here is a function that returns square of a number.
我们还可以在irb
定义函数。 函数是一组执行特定任务的语句。 这是一个返回数字平方的函数。
def square(n)
return n * n
end
Calling the function: square (4)
调用函数: square (4)
Calling this function and passing 4 as argument returns 16 by performing the operation 4 * 4
. Likewise, a function to find the area of a circle.
调用此函数并将4作为参数传递,通过执行操作4 * 4
返回16 。 同样,该功能可以查找圆的面积。
def circle(radius)
return radius * radius * 3.14
end
Calling the function: circle (5)
and passing 5 as argument. This function returns 78.5 by multiplying 5 * 5 * 3.14
调用函数: circle (5)
并传递5作为参数。 该函数通过乘以5 * 5 * 3.14
返回78.5
Ruby脚本 (Ruby Scripts)
We are going to run ruby from a text editor rather that a Command Prompt. You can use any text editor like Notepad, Notepad++, Sublime, Brackets. Ruby scripts have .rb extension. So, you have to save the file with the extension .rb
. Now, in your editor enter the following code and save it with any name with .rb
extension:
我们将通过文本编辑器而不是命令提示符来运行ruby。 您可以使用任何文本编辑器,例如Notepad , Notepad ++ , Sublime , Brackets 。 Ruby脚本的扩展名为.rb。 因此,您必须保存扩展名为.rb
的文件。 现在,在编辑器中输入以下代码,并将其保存为扩展名为.rb
任何名称:
puts "Welcome to Ruby!"
Let's save this as first.rb. To run the program, go to Command Prompt, and type ruby first.rb
. You can also run by simply typing first.rb
as well, if during installation, "Add Ruby executables to your PATH" option was checked.
让我们将其另存为first.rb 。 要运行该程序,请转到命令提示符,然后键入ruby first.rb
。 如果在安装过程中选中了“将Ruby可执行文件添加到PATH”选项,则也可以通过直接键入first.rb
来运行。

Likewise, you can use any text editor in Macintosh and follow the same procedures as in Windows.
同样,您可以在Macintosh中使用任何文本编辑器,并按照与Windows中相同的步骤进行操作。
ruby执行shell命令