如何运行在Mac OS终端的文本编辑器中创建的Ruby脚本?

时间:2022-04-19 01:11:55

I just started reading the Well-Grounded Rubyist, and I am just beginning to use Ruby in my terminal on my Mac.

我刚刚开始阅读有良好基础的Rubyist,我开始在我的Mac终端上使用Ruby。

I'm on the very first lesson, creating a Celsius to Farenheit converter in a text editor. I've saved the code as an .rb file by using Textmate (my text editor). The file name is c2f.rb. The file is saved in a folder on my desktop titled "Rubycode".

我在第一堂课上,在一个文本编辑器中创建一个摄氏度到华氏度的转换器。我使用Textmate(我的文本编辑器)将代码保存为.rb文件。文件名是c2f.rb。这个文件被保存在我桌面上一个名为“Rubycode”的文件夹中。

I am having difficulty running the .rb file in the terminal however. I've tried many different methods of trying to call the file, including using:

但是,我在终端上运行.rb文件有困难。我尝试了很多不同的方法来调用这个文件,包括:

cd /Users/rexrose/Desktop/Rubycode/c2f

and many others.

和许多其他人。

Any thoughts on what exactly, I'm supposed to type into terminal in order to call the c2f file?

有什么想法吗,我应该输入终端以调用c2f文件?

Thanks in advance.

提前谢谢。

2 个解决方案

#1


6  

I just started reading Well-Grounded Rubyist.

我刚开始读一些有根据的Rubyist。

That's a very good book. I consider it more of an intermediate level book than a beginner book, but no matter.

那是一本很好的书。我认为它更像是一本中级水平的书,而不是一本初学者的书,但没关系。

I've tried many different methods of trying to call the file, including using

我尝试了许多不同的方法来尝试调用这个文件,包括使用

cd /Users/rexrose/Desktop/Rubycode/c2f

The cd command means "change directories" and you cannot change directories to a file. Instead, you have to change directories to the directory containing the file:

cd命令表示“更改目录”,不能将目录更改为文件。相反,您必须将目录更改为包含文件的目录:

$ cd /Users/rexrose/Desktop/Rubycode

Then you can execute your program contained in the file c2f.rb like this:

然后可以执行文件c2f中包含的程序。rb:

$ ruby c2f.rb

Here are some Terminal tips:

以下是一些终端提示:

1) You can use ~ instead of /Users/YourUserName, so you can save some typing by doing this:

1)你可以使用~而不是/Users/YourUserName,这样你就可以保存一些输入:

$ cd ~/Desktop/Rubycode

Typing '~' instead of '/Users/YourUserName' will become second nature.

键入“~”而不是“/Users/YourUserName”将成为第二天性。

2) Using the cd command with no arguments:

2)使用无参数的cd命令:

$ cd

will take you to your home directory, i.e. /Users/YourUserName

将您带到您的主目录,即/Users/YourUserName

3) You should change your prompt to indicate what directory you are currently in. To do that, create a file called .bash_profile in your home directory(/Users/YourUserName). Check to see if it exists first:

您应该更改提示符以指示当前所在的目录。为此,在主目录(/Users/YourUserName)中创建一个名为.bash_profile的文件。先看看它是否存在:

$ cd
$ ls -al

The command ls -al will show all the files in a directory, including hidden files, which are files whose name begins with a .. If a file named .bash_profile exists, open it; if it doesn't exist, create it. Put this in .bash_profile:

命令ls -al将显示一个目录中的所有文件,包括隐藏文件,这些文件的名称以a开头。如果存在一个名为.bash_profile的文件,则打开它;如果它不存在,就创建它。. bash_profile中把这个:

PS1="\w$ "

To get Terminal to recognize the changes, you can either Quit Terminal and relaunch it, or do this:

要让终端识别更改,您可以退出终端重新启动它,或者这样做:

$ source .bash_profile

Then open a new Terminal widow.

然后打开一个新的终端寡妇。

You can also add 'aliases' to .bash_profile. For instance, in my .bash_profile I have the alias 'r' for 'ruby', so that I can execute a ruby program like this:

您还可以向.bash_profile添加“别名”。例如,在我的.bash_profile中,我有“ruby”的别名“r”,这样我就可以执行这样的ruby程序:

$ r my_program.rb

In .bash_profile you make an alias like this:

在.bash_profile中,您可以创建如下的别名:

alias r="ruby"

4) Tab completion in Terminal:

4)终端制表完成:

You might have noticed that you can type part of a file name, then hit tab and Terminal will complete the file name. Using tab completion, I can execute my ruby program like this:

您可能已经注意到,您可以键入文件名的一部分,然后单击tab和Terminal将完成文件名。使用tab补全,我可以像这样执行我的ruby程序:

$ r my_pr<tab>

In fact, I name my practice ruby programs so that I can use tab completion to the greatest effect. I have files named 1.rb, 2.rb, 3.rb, and then I execute one of them by simply typing:

事实上,我把我的练习命名为ruby程序,这样我就可以使用tab补全来达到最大的效果。我有一个名为1的文件。rb,2。rb,3。rb,然后我只需要输入

$ r 1<tab>

And in fact, you may not even have to type that! If you hit the up arrow key on your keyboard, Terminal will display the previous command, and if you hit the up arrow key again, you will see the command before that. So you can scroll up to a previous command, then hit return to execute it--without having to type anything.

事实上,你甚至不需要输入它!如果您在键盘上按向上箭头键,终端将显示先前的命令,如果您再次按向上箭头键,您将看到前面的命令。因此,您可以向上滚动到以前的命令,然后单击return来执行它——无需输入任何内容。

You should endeavor to use tab completion for each of the file names in a path. For example, if you are cd'ing to /Users/YourUserName/dir1/dir2, you should do this:

您应该努力为路径中的每个文件名使用tab补全。例如,如果你要cd到/Users/YourUserName/dir1/dir2,你应该这样做:

$ cd /Use<tab>/YourUser<tab>/di<tab>/di<tab>

The reason you should use tab completion for each filename(by the way in Unix filename is a general term for both directory names and file names) is because when the name won't tab complete, then you are in the wrong directory or you are trying a filename that doesn't exist in that directory. So instead of having to type out the whole path '/Users/YourUserName/dir1/dir2' and then finding out about the error when you hit return, the tab completion will let you know immediately when there is an error(because the filename won't tab complete)--saving you some typing.

原因你应该使用选项卡完成每个文件名(顺便在Unix文件名是一个一般术语为目录名和文件名)是因为当这个名字不会选项卡完成,那么你是在错误的目录或你正在一个文件名不存在的目录。因此,当你点击返回时,不需要键入整个路径'/用户/YourUserName/dir1/dir2',然后找出错误,当出现错误时,选项卡完成会让你立即知道(因为文件名不完整)——为你节省了一些输入。

5) Because you will probably be using Terminal for mostly ruby programs for awhile, you can set up things so that Terminal will automatically open up in your directory Users/rexrose/Desktop/Rubycode. Put this in .bash_profile:

5)因为您可能会在大多数ruby程序中使用终端机一段时间,所以您可以设置一些东西,以便在目录用户/rexrose/Desktop/Rubycode中自动打开终端机。. bash_profile中把这个:

cd "/Users/rexrose/Desktop/Rubycode"  (Here you cannot use ~)

6) Occasionally, you may have to type a long file name that exists on your computer into the command line:

6)偶尔,您可能需要将计算机上存在的一个长文件名输入到命令行:

$ cd /Library/SomeLongName/AnotherLongName34832o222/142582dir/some_file.txt

Instead of having to type all that at the command line, you can locate the file in Finder first. Then if you drag the file onto the Terminal window, the file name will be entered at the point of the cursor.

不必在命令行中输入所有这些,您可以先在Finder中找到文件。然后,如果将文件拖到终端窗口,将在游标的位置输入文件名。

Finally, a better way to organize your files might be to put them in directories below your home directory, like this:

最后,更好的组织文件的方法可能是将它们放在主目录下面的目录中,如下所示:

~$ mkdir ruby_programs
~$ cd ruby_programs
~/ruby_programs$ mate 1.rb

#2


1  

First things first: cd stands for "Change directory".

首先,cd代表“更改目录”。

Normally the terminal should open in "~", which is the home directory where most of your things are. In OS X it will be /Users/[username]. It's also possible possible that in OS X, it will save the location of the last session. I also recommend, since you're starting to install, "Iterm2", which is a nice terminal to use. It supports multiple tabs, etc.

通常,终端应该在“~”中打开,“~”是您的大部分内容所在的主目录。在OS X中是/Users/[username]。在OS X中,也有可能保存上一个会话的位置。我还建议,因为您正在开始安装“Iterm2”,这是一个很好的终端。它支持多个选项卡等等。

Ruby, the interpreter, is the command "ruby". To call a script you have to call Ruby with a filename:

解释器Ruby是“Ruby”命令。要调用脚本,您必须使用文件名调用Ruby:

ruby /Users/rexrose/Desktop/Rubycode/c2f/c2f.rb

That is almost the equivalent of:

这几乎相当于:

cd /Users/rexrose/Desktop/Rubycode/c2f/
ruby c2f.rb

It's almost equivalent, but for now the difference shouldn't bother you. Let say that the second way to call the script is more favorable than the first.

这几乎是等价的,但是现在的差异不应该困扰你。让我们说第二个调用脚本的方法比第一个更有利。

Now, the second thing: If you want to try things in Ruby, you can start an interactive shell. The command is "irb".

现在,第二件事:如果您想在Ruby中尝试一些东西,您可以启动一个交互式shell。该命令“irb”。

Type irb and Enter and then you can type Ruby code. If you want to leave, press CTRL+C multiple times.

输入irb,然后输入Ruby代码。如果你想离开,按CTRL+C多次。

The last thing, I recommend installing "RVM". It will save you time and pain, I hope. If you want to install Ruby gems, it will not mess with the Ruby already present with the system. That's my personal opinion but I believe lots of people will agree. Even if Ruby comes with OS X, you should install a different Ruby for development. It will make sure that if something goes wrong in dev, it will not mess the Ruby OS X might be using.

最后,我建议安装“RVM”。我希望这能节省你的时间和痛苦。如果您想要安装Ruby gems,它不会扰乱系统中已经存在的Ruby。这是我个人的看法,但我相信很多人都会同意。即使Ruby附带OS X,也应该安装不同的Ruby进行开发。它将确保如果开发中出现问题,不会影响Ruby OS X的使用。

#1


6  

I just started reading Well-Grounded Rubyist.

我刚开始读一些有根据的Rubyist。

That's a very good book. I consider it more of an intermediate level book than a beginner book, but no matter.

那是一本很好的书。我认为它更像是一本中级水平的书,而不是一本初学者的书,但没关系。

I've tried many different methods of trying to call the file, including using

我尝试了许多不同的方法来尝试调用这个文件,包括使用

cd /Users/rexrose/Desktop/Rubycode/c2f

The cd command means "change directories" and you cannot change directories to a file. Instead, you have to change directories to the directory containing the file:

cd命令表示“更改目录”,不能将目录更改为文件。相反,您必须将目录更改为包含文件的目录:

$ cd /Users/rexrose/Desktop/Rubycode

Then you can execute your program contained in the file c2f.rb like this:

然后可以执行文件c2f中包含的程序。rb:

$ ruby c2f.rb

Here are some Terminal tips:

以下是一些终端提示:

1) You can use ~ instead of /Users/YourUserName, so you can save some typing by doing this:

1)你可以使用~而不是/Users/YourUserName,这样你就可以保存一些输入:

$ cd ~/Desktop/Rubycode

Typing '~' instead of '/Users/YourUserName' will become second nature.

键入“~”而不是“/Users/YourUserName”将成为第二天性。

2) Using the cd command with no arguments:

2)使用无参数的cd命令:

$ cd

will take you to your home directory, i.e. /Users/YourUserName

将您带到您的主目录,即/Users/YourUserName

3) You should change your prompt to indicate what directory you are currently in. To do that, create a file called .bash_profile in your home directory(/Users/YourUserName). Check to see if it exists first:

您应该更改提示符以指示当前所在的目录。为此,在主目录(/Users/YourUserName)中创建一个名为.bash_profile的文件。先看看它是否存在:

$ cd
$ ls -al

The command ls -al will show all the files in a directory, including hidden files, which are files whose name begins with a .. If a file named .bash_profile exists, open it; if it doesn't exist, create it. Put this in .bash_profile:

命令ls -al将显示一个目录中的所有文件,包括隐藏文件,这些文件的名称以a开头。如果存在一个名为.bash_profile的文件,则打开它;如果它不存在,就创建它。. bash_profile中把这个:

PS1="\w$ "

To get Terminal to recognize the changes, you can either Quit Terminal and relaunch it, or do this:

要让终端识别更改,您可以退出终端重新启动它,或者这样做:

$ source .bash_profile

Then open a new Terminal widow.

然后打开一个新的终端寡妇。

You can also add 'aliases' to .bash_profile. For instance, in my .bash_profile I have the alias 'r' for 'ruby', so that I can execute a ruby program like this:

您还可以向.bash_profile添加“别名”。例如,在我的.bash_profile中,我有“ruby”的别名“r”,这样我就可以执行这样的ruby程序:

$ r my_program.rb

In .bash_profile you make an alias like this:

在.bash_profile中,您可以创建如下的别名:

alias r="ruby"

4) Tab completion in Terminal:

4)终端制表完成:

You might have noticed that you can type part of a file name, then hit tab and Terminal will complete the file name. Using tab completion, I can execute my ruby program like this:

您可能已经注意到,您可以键入文件名的一部分,然后单击tab和Terminal将完成文件名。使用tab补全,我可以像这样执行我的ruby程序:

$ r my_pr<tab>

In fact, I name my practice ruby programs so that I can use tab completion to the greatest effect. I have files named 1.rb, 2.rb, 3.rb, and then I execute one of them by simply typing:

事实上,我把我的练习命名为ruby程序,这样我就可以使用tab补全来达到最大的效果。我有一个名为1的文件。rb,2。rb,3。rb,然后我只需要输入

$ r 1<tab>

And in fact, you may not even have to type that! If you hit the up arrow key on your keyboard, Terminal will display the previous command, and if you hit the up arrow key again, you will see the command before that. So you can scroll up to a previous command, then hit return to execute it--without having to type anything.

事实上,你甚至不需要输入它!如果您在键盘上按向上箭头键,终端将显示先前的命令,如果您再次按向上箭头键,您将看到前面的命令。因此,您可以向上滚动到以前的命令,然后单击return来执行它——无需输入任何内容。

You should endeavor to use tab completion for each of the file names in a path. For example, if you are cd'ing to /Users/YourUserName/dir1/dir2, you should do this:

您应该努力为路径中的每个文件名使用tab补全。例如,如果你要cd到/Users/YourUserName/dir1/dir2,你应该这样做:

$ cd /Use<tab>/YourUser<tab>/di<tab>/di<tab>

The reason you should use tab completion for each filename(by the way in Unix filename is a general term for both directory names and file names) is because when the name won't tab complete, then you are in the wrong directory or you are trying a filename that doesn't exist in that directory. So instead of having to type out the whole path '/Users/YourUserName/dir1/dir2' and then finding out about the error when you hit return, the tab completion will let you know immediately when there is an error(because the filename won't tab complete)--saving you some typing.

原因你应该使用选项卡完成每个文件名(顺便在Unix文件名是一个一般术语为目录名和文件名)是因为当这个名字不会选项卡完成,那么你是在错误的目录或你正在一个文件名不存在的目录。因此,当你点击返回时,不需要键入整个路径'/用户/YourUserName/dir1/dir2',然后找出错误,当出现错误时,选项卡完成会让你立即知道(因为文件名不完整)——为你节省了一些输入。

5) Because you will probably be using Terminal for mostly ruby programs for awhile, you can set up things so that Terminal will automatically open up in your directory Users/rexrose/Desktop/Rubycode. Put this in .bash_profile:

5)因为您可能会在大多数ruby程序中使用终端机一段时间,所以您可以设置一些东西,以便在目录用户/rexrose/Desktop/Rubycode中自动打开终端机。. bash_profile中把这个:

cd "/Users/rexrose/Desktop/Rubycode"  (Here you cannot use ~)

6) Occasionally, you may have to type a long file name that exists on your computer into the command line:

6)偶尔,您可能需要将计算机上存在的一个长文件名输入到命令行:

$ cd /Library/SomeLongName/AnotherLongName34832o222/142582dir/some_file.txt

Instead of having to type all that at the command line, you can locate the file in Finder first. Then if you drag the file onto the Terminal window, the file name will be entered at the point of the cursor.

不必在命令行中输入所有这些,您可以先在Finder中找到文件。然后,如果将文件拖到终端窗口,将在游标的位置输入文件名。

Finally, a better way to organize your files might be to put them in directories below your home directory, like this:

最后,更好的组织文件的方法可能是将它们放在主目录下面的目录中,如下所示:

~$ mkdir ruby_programs
~$ cd ruby_programs
~/ruby_programs$ mate 1.rb

#2


1  

First things first: cd stands for "Change directory".

首先,cd代表“更改目录”。

Normally the terminal should open in "~", which is the home directory where most of your things are. In OS X it will be /Users/[username]. It's also possible possible that in OS X, it will save the location of the last session. I also recommend, since you're starting to install, "Iterm2", which is a nice terminal to use. It supports multiple tabs, etc.

通常,终端应该在“~”中打开,“~”是您的大部分内容所在的主目录。在OS X中是/Users/[username]。在OS X中,也有可能保存上一个会话的位置。我还建议,因为您正在开始安装“Iterm2”,这是一个很好的终端。它支持多个选项卡等等。

Ruby, the interpreter, is the command "ruby". To call a script you have to call Ruby with a filename:

解释器Ruby是“Ruby”命令。要调用脚本,您必须使用文件名调用Ruby:

ruby /Users/rexrose/Desktop/Rubycode/c2f/c2f.rb

That is almost the equivalent of:

这几乎相当于:

cd /Users/rexrose/Desktop/Rubycode/c2f/
ruby c2f.rb

It's almost equivalent, but for now the difference shouldn't bother you. Let say that the second way to call the script is more favorable than the first.

这几乎是等价的,但是现在的差异不应该困扰你。让我们说第二个调用脚本的方法比第一个更有利。

Now, the second thing: If you want to try things in Ruby, you can start an interactive shell. The command is "irb".

现在,第二件事:如果您想在Ruby中尝试一些东西,您可以启动一个交互式shell。该命令“irb”。

Type irb and Enter and then you can type Ruby code. If you want to leave, press CTRL+C multiple times.

输入irb,然后输入Ruby代码。如果你想离开,按CTRL+C多次。

The last thing, I recommend installing "RVM". It will save you time and pain, I hope. If you want to install Ruby gems, it will not mess with the Ruby already present with the system. That's my personal opinion but I believe lots of people will agree. Even if Ruby comes with OS X, you should install a different Ruby for development. It will make sure that if something goes wrong in dev, it will not mess the Ruby OS X might be using.

最后,我建议安装“RVM”。我希望这能节省你的时间和痛苦。如果您想要安装Ruby gems,它不会扰乱系统中已经存在的Ruby。这是我个人的看法,但我相信很多人都会同意。即使Ruby附带OS X,也应该安装不同的Ruby进行开发。它将确保如果开发中出现问题,不会影响Ruby OS X的使用。