Ruby:EventMachine 未加载

发布于 2024-11-24 08:09:13 字数 976 浏览 4 评论 0 原文

更新:

我找到了简单的答案,这完全是我自己的错:我将上述代码存储在名为“eventmachine.rb”的文件中。因此,当我需要“eventmachine”时,它只是导入相同的文件,其中不包含 EventMachine 的定义。

所以这并不是一篇关于 EventMachine 的文章。

原帖:

请问,这是怎么回事?

require 'rubygems'
require 'eventmachine'

EventMachine

都会给出

uninitialized constant EventMachine

当我运行“ruby myfile.rb”时,在 TextMate 和控制台上

。但如果我在 IRB 中输入相同的内容,我会得到

=> EventMachine

.

...

我非常沮丧,以至于我按照 http://blog.carlmercier.com/2007/12/14/how-to-fix-that-rubygems-mess-on-leopard/ -- 清除我的 Ruby 和 Rubygems环境并从 MacPorts 重新安装它们。 IRB 仍然有效,TextMate/命令行仍然以同样的方式失败。我什至将 TextMate 路径减少到现在安装 Ruby、Gem 和 IRB 的位置:/opt/local/bin——这就是 Which 报告的内容。

最后,当我

puts Gem.path

在 IRB、TextMate 或控制台上时,我得到相同的路径。

UPDATE:

I found the simple answer and it's completely my own fault: I stored the above code in a file called "eventmachine.rb". Thus when I required 'eventmachine' it just imported the same file, which does not contain a definition for EventMachine.

So this is not really a post about EventMachine.

ORIGINAL POST:

Excuse me, but what's going on?

require 'rubygems'
require 'eventmachine'

EventMachine

gives

uninitialized constant EventMachine

in both TextMate and on the console when I run "ruby myfile.rb".

But if I type the same thing into IRB, I get

=> EventMachine

.

...

I got so frustrated that I followed the rash instructions on http://blog.carlmercier.com/2007/12/14/how-to-fix-that-rubygems-mess-on-leopard/ -- wiping out my Ruby and Rubygems environments and reinstalling them from MacPorts. IRB still works and TextMate/commandline still fails in the same way. I even reduced my TextMate PATH to the one place where Ruby, Gem, and IRB are installed now: /opt/local/bin -- which is what Which reports.

Finally, when I

puts Gem.path

on either IRB, TextMate, or the console, I get the same path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

财迷小姐 2024-12-01 08:09:13

我找到了简单的答案,这完全是我自己的错:
我将上述代码存储在名为“eventmachine.rb”的文件中。因此,当我需要“eventmachine”时,它只是导入相同的文件,其中不包含 EventMachine 的定义。

I found the simple answer and it's completely my own fault:
I stored the above code in a file called "eventmachine.rb". Thus when I required 'eventmachine' it just imported the same file, which does not contain a definition for EventMachine.

坐在坟头思考人生 2024-12-01 08:09:13

你安装了gem吗?

gem install eventmachine

通常,像您显示的代码中那样的错误是因为该行

require 'eventmachine'

失败并且未定义类/模块常量 EventMachine。

另外,如果您运行的是 ruby​​ 1.9,则不再需要

require 'rubygems'

Have you installed the gem?

gem install eventmachine

Typically an error like that in the code you show is because the line

require 'eventmachine'

failed and that class/module constant, EventMachine, is not defined.

Also, if you are running ruby 1.9, you no longer need to have

require 'rubygems'
许久 2024-12-01 08:09:13

我不明白为什么消息是“未初始化常量”,但问题是您没有提供任何运行内容。我在你的代码中遇到了同样的错误,但如果我将其更改为:

EventMachine::run{
   EventMachine::add_periodic_timer( 0.1) { 
    print '.'
   }
}

那么它的工作原理完全符合我的预期 - 它每秒打印 10 点。

I don't understand why the message is "Uninitialized Constant", but the problem is that you provided nothing to run. I get the same error with your code, but if I change it to:

EventMachine::run{
   EventMachine::add_periodic_timer( 0.1) { 
    print '.'
   }
}

then it works exactly as I expect - it prints 10 dots per second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文