NameError:未定义的局部变量或irb中的方法

时间:2022-02-22 11:55:55

I am using ruby 1.9.2 with irb and I keep getting the following error message:

我正在使用带有irb的ruby 1.9.2并且我不断收到以下错误消息:

NameError: undefined local variable or method `ex1' for main:Object
from (irb):4
from /Users/flexmaster411/.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>' 

I have also tried to drag and drop into irb then I get another argument error the code.

我也尝试拖放到irb然后我得到另一个参数错误的代码。

Rodas-MacBook-Air:~ flexmaster411$ pwd /Users/flexmaster411
Rodas-MacBook-Air:~ flexmaster411$ cd Desktop
Rodas-MacBook-Air:Desktop flexmaster411$ cd my
Rodas-MacBook-Air:my flexmaster411$ ls
ex1.rb ex2.rb   test.html   test.rb  wacky.rb
Rodas-MacBook-Air:my flexmaster411$ irb
1.9.2-p320 :001 > ruby ex1.rb
NameError: undefined local variable or method ex1' for main:Object from (irb):1 from /Users/flexmaster411/.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in <main>' 

1 个解决方案

#1


3  

So what you were trying to do was:

所以你想要做的是:

$ irb
001 > ruby ex1.rb

The ruby command is a program on its own, so you should use ruby directly from the command line like this:

ruby命令本身就是一个程序,所以你应该直接从命令行使用ruby,如下所示:

$ ruby ex1.rb

This means “Ruby, please execute this file”, whereas irb is a REPL, awaiting Ruby statements directly. This means you could type your Ruby code directly into irb like this:

这意味着“Ruby,请执行此文件”,而irb是一个REPL,直接等待Ruby语句。这意味着您可以将Ruby代码直接输入到irb中,如下所示:

$ irb
001 > puts "Hello, world!"
Hello, world!

Or you could go into irb and load the contents of the file and then experiment with the code:

或者你可以进入irb并加载文件的内容然后试验代码:

$ irb
001 > require './ex1'

#1


3  

So what you were trying to do was:

所以你想要做的是:

$ irb
001 > ruby ex1.rb

The ruby command is a program on its own, so you should use ruby directly from the command line like this:

ruby命令本身就是一个程序,所以你应该直接从命令行使用ruby,如下所示:

$ ruby ex1.rb

This means “Ruby, please execute this file”, whereas irb is a REPL, awaiting Ruby statements directly. This means you could type your Ruby code directly into irb like this:

这意味着“Ruby,请执行此文件”,而irb是一个REPL,直接等待Ruby语句。这意味着您可以将Ruby代码直接输入到irb中,如下所示:

$ irb
001 > puts "Hello, world!"
Hello, world!

Or you could go into irb and load the contents of the file and then experiment with the code:

或者你可以进入irb并加载文件的内容然后试验代码:

$ irb
001 > require './ex1'