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
开发者_如何学Go
.
...
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.
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.
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'
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.
精彩评论