Ruby gem不会在独立的ruby脚本中加载

时间:2013-11-20 03:38:47

标签: ruby rubygems arduino

我正在尝试创建一个简单的ruby应用程序来使用Arduino。我的问题是没有找到Ruby Gems。这是我的脚本代码 - 它来自我发现的教程:

#!/usr/bin/ruby -rubygems

require 'rubygems'
require 'serialport'
require 'gmail'

#plug in your username and password here
gmail = Gmail.connect("username", "password")

#count the number of unread messages
prev_unread = gmail.inbox.count(:unread)

#this *will* be different for you
#You need to find out what port your arduino is on
#and also what the corresponding file is on /dev
#You can do this by looking at the bottom right of the Arduino
#environment which tells you what the path.
port_file = '/dev/tty.usbmodem1421'

#this must be same as the baud rate set on the Arduino
#with Serial.begin
baud_rate = 9600

data_bits = 8
stop_bits = 1
parity = SerialPort::NONE

#create a SerialPort object using each of the bits of information
port = SerialPort.new(port_file, baud_rate, data_bits, stop_bits, parity)

wait_time = 4

#for an infinite amount of time
loop do
  #get the number of unread messages in the inbox
  unread = gmail.inbox.count(:unread)

  #lets us know that we've checked the unread messages
  puts "Checked unread."

  #check if the number of unread messages has increased
  #if so, we have a new email! So, blink the LED.
  if unread > prev_unread
    port.write "b"
  end

  #reset the number of unread emails
  prev_unread = unread

  #wait before we make another request to the Gmail servers
  sleep wait_time
end

然后我转到此文件(gmail_notifier.rb)所在的目录并运行ruby gmail_notifier.rb。这是我在运行该命令后收到的错误:

/Users/ryan/.rvm/rubies/ruby-2.0.0-head/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require': cannot load such file -- serialport (LoadError)
    from /Users/ryan/.rvm/rubies/ruby-2.0.0-head/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
    from gmail_notifier.rb:4:in `<main>'

任何人都知道为什么没有找到宝石?我确实已经运行gem install gmailgem install serialport

1 个答案:

答案 0 :(得分:1)

请参阅here,您需要:

gem 'gmail'
gem 'serialport'

在您的要求线之前。 (只要你在ruby&gt; 1.8)