Ruby代码不起作用

时间:2014-05-19 22:38:23

标签: ruby xcode macos ubuntu

我从一位同事那里继承了一些代码,我以前没有完成编码。此代码适用于我的机器和其他机器。但是,当我尝试在新安装的Ruby和依赖宝石的新系统上运行它时,它不会按预期执行。所以这段代码可以工作......而不是我们所有的系统。而且我太陌生,不知道究竟要找到什么来找到不一致。

如果有人对我如何解决这个问题有任何想法或提示,我将非常感激。

require 'curb' # For Curl statement
require 'nokogiri' # for screen scrapping
require 'io/console' # hide password input
require 'mechanize' # clicking through and logging in

#start your engines
agent = Mechanize.new
agent.keep_alive = false
agent.open_timeout   = 180
agent.read_timeout   = 180
page  = agent.get('http://_____.co.__________.com/admin/_____/user/')

#get login for _____
puts "Please enter your _____ Login:"
prompt = '>'
print prompt
page.forms.first.fields[1].value =  STDIN.gets.chomp()
puts "Please enter your password:"
print prompt
#this way keeps you from seeing the password on the screen
page.forms.first.fields[2].value =  STDIN.noecho(&:gets).chomp()
puts "Searching for %s" % ARGV[0]
#click the login button
login_page = page.forms.first.submit    
#take the first argument and put it in the user search box
login_page.forms.first.fields.first.value = ARGV[0]
#click search
users_page = login_page.forms.first.submit
#click on the only link, people with "deleted" account might need some hacking to make work
inventory_page = users_page.links.last.click
puts "Found %s, now opening user page..." % ARGV[0]
active_inventory = []
puts "checking total active inventorys..."
#find the inventory table and get all the rows. Then loop through and check if active. If active put FSID into an array to check next
inventory_page.parser.xpath('//tr[contains(@id,"filesystemproperties")]').each do |row|  
#inventory_page.parser.css('tr.has_original').each do |row|     
    if row.at('img')["alt"] == "True"
        active_inventory << row.children[2].text.gsub("\n",'').strip
        puts row.children[2].text.gsub("\n",'').strip
    end
end

#now lets send a inventory, go to the inventory Versions link at the top of the page
get_inventory = inventory_page.links[3].click

#require 'logger' #debug stuff, pay no attention.
#agent.log = Logger.new $stderr
#agent.agent.http.debug_output = $stderr
puts "checking total size of inventorys..."

#time for some simple addition
inventory_size = 0
#clear out any blank fsid, then each one send (POST)
active_inventory.reject!(&:empty?).each do |fsid|
    get_inventory.forms.first.fields.first.value = fsid
    size = get_inventory.forms.first.submit
    #get the top row second column for latest size, and add it to the existing tally for total size
    inventory_size += size.parser.xpath('//tbody/tr[1]/td[2]').text.to_i
    puts "%s is %d Bytes" % [fsid, size.parser.xpath('//tbody/tr[1]/td[2]').text.to_i]
    puts "current inventory: #{inventory_size}"
    #sleep 15
end
#convert those bytes to MEGABytes
puts "%d currently active inventory(s) found\n" % (active_inventory.count - 3)
puts "\n \n \nTotal inventory size is: %f MB" % [inventory_size/(1024*1024).to_f]

您可以在附加的图像中看到输出。 紫色背景正在运行,白色背景已被破坏。我在Ubuntu VM上也有同样不成功的结果

工作: OS X 10.8.5; Ruby 2.1.1; XCODE 5.1.1 不能工作:OS X 10.8.5(VirtualBox); Ruby 2.1.1; XCODE 5.1.1 不能工作:Ubuntu 14.04(VirualBox); Ruby 2.1.2; XCODE不适用

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:4)

环境的差异,特别是与宝石的差异,可能导致行为不一致。在每个环境中,尝试

gem list

查找此脚本所需的gem,并在不同环境之间查找不同的版本号。如果确实需要不同的版本,可以执行以下操作:

gem uninstall <gem-name>
gem install <gem-name> -v <desired-version-number>