如何保存一个ruby文件? (错误)(How do I save a ruby file? (e

2019-10-18 11:04发布

我收到的错误在我的命令行没有这样的文件或目录,我想这是因为我没有保存Ruby文件放在特定的。 我所做的只是创建一个随机文件夹,在那里我会救我的Ruby文件。

我需要拯救我的脚本在原来的红宝石文件夹? 谢谢!

**这是Windows 7操作系统。

更多信息 - 我所做的只是做一个名为“Matz.rb”因为我目前正在读O'Reilly的红宝石书一个简单的文件。 在我的代码的所有我写的是看跌期权“你好马茨”。 我救了这个我的桌面上。 当我去命令行,而我写的红宝石matz.rb它说:“红宝石:没有这样的文件或目录 - matz.rb”请帮助:(

如果有事情做与PATH或炮弹,说实话,我不知道那些真的是因为刚开始我昨晚编码。

Answer 1:

You are most likely not in the right folder. You somehow need to tell the ruby interpreter where it is looking for the file.

Either make sure you're in the right folder - the cd command allows you to change location:

cd C:\Users\Username\Desktop
ruby Matz.rb

or specify the path explicitly:

ruby C:\Users\Username\Desktop\Matz.rb

By default, the ruby interpreter will look in your current directory (the location shown in your prompt) for whatever filename you give it.


Edit: I'll attempt to explain what I mean step-by-step.

  • When you initially open the command prompt, it will indicate what folder you are in (your "current working directory") in the prompt:

    C:\Users\YourUsername >
    

    In the above example, you are working in the C:\Users\YourUsername folder.

  • You can move directories using the cd command. For example, typing cd Desktop moves you into the folder called Desktop, assuming such a folder exists in your current location
  • You can move to another folder outside your current folder by specifying explicitly where you want to be: cd C:\Another\Place
  • When you run a ruby command such as ruby Matz.rb, the system knows how to find the ruby program because the installer placed its location into the PATH environment variable. Don't worry about this too much, this just explains the "magic" by which it knows what ruby means, no matter where you are.
  • Ruby finds the file you specify (in the above example, Matz.rb) by looking in the current directory. To re-iterate, it is looking in whatever folder is written right there in your prompt.
  • You can tell ruby to look outside the current folder by specifying the full path (as shown in the answer above).

To go from a new command window that you've just opened, to typing ruby Matz.rb and having it work, you need to do the following:

  1. Move to the correct directory
  2. Run the command

If we assume your username is alex and you have a folder on your desktop called "rubycode", which contains Matz.rb, you could do this:

  1. Open a command prompt, which will most likely start in C:\Users\Alex
  2. Move to the rubycode folder on your desktop: cd Desktop\rubycode. All subsequent commands will be working from within this folder.
  3. Run the ruby command, specifying the file: ruby Matz.rb
  4. Continue to run ruby commands as you learn ruby.

I hope that makes sense.



文章来源: How do I save a ruby file? (error)