Ruby代码不工作

Ruby代码不工作,ruby,xcode,macos,ubuntu,Ruby,Xcode,Macos,Ubuntu,我从一位同事那里继承了一些代码,问题是我以前没有做过编码。此代码在我的机器和其他机器上工作。然而,当我尝试在一个新系统上运行它时,使用新安装的Ruby和相关的gems,它并没有像预期的那样执行。所以这个代码确实有效。。。只是不是在我们所有的系统上。我太不熟悉了,不知道到底要找什么来找出不一致性 如果有人对我如何解决这个问题有什么想法或建议,我将非常感激 require 'curb' # For Curl statement require 'nokogiri' # for screen scra

我从一位同事那里继承了一些代码,问题是我以前没有做过编码。此代码在我的机器和其他机器上工作。然而,当我尝试在一个新系统上运行它时,使用新安装的Ruby和相关的gems,它并没有像预期的那样执行。所以这个代码确实有效。。。只是不是在我们所有的系统上。我太不熟悉了,不知道到底要找什么来找出不一致性

如果有人对我如何解决这个问题有什么想法或建议,我将非常感激

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]
Curl语句需要“加铺”
需要“nokogiri”进行屏幕报废
需要“io/控制台”#隐藏密码输入
需要“机械化”#点击并登录
#启动你的引擎
agent=Mechanize.new
agent.keep_alive=false
agent.open\u超时=180
agent.read\u超时=180
page=agent.get('http://_____.co.__________.com/admin/_____/user/')
#获取的登录名_____
放置“请输入您的登录名:”
prompt='>'
打印提示
page.forms.first.fields[1]。value=STDIN.gets.chomp()
放入“请输入您的密码:”
打印提示
#这样可以防止您在屏幕上看到密码
page.forms.first.fields[2].value=STDIN.noecho(&:gets.chomp())
将“搜索%s”%ARGV[0]
#单击登录按钮
登录页面=page.forms.first.submit
#获取第一个参数并将其放入用户搜索框中
login_page.forms.first.fields.first.value=ARGV[0]
#单击搜索
用户\页面=登录\页面.forms.first.submit
#点击唯一的链接,拥有“已删除”帐户的人可能需要一些黑客才能工作
inventory\u page=用户\u page.links.last.click
放置“找到%s,正在打开用户页…”%ARGV[0]
活动库存=[]
放入“正在检查总的活动库存…”
#找到库存表并获取所有行。然后循环并检查是否处于活动状态。如果处于活动状态,请将FSID放入阵列中以进行下一步检查
inventory_page.parser.xpath('//tr[contains(@id,“filesystemproperties”)]')。每个do |行|
#inventory_page.parser.css('tr.has_original')。每个do|row|
如果行.at('img')[“alt”]=“True”

活跃的库存环境的差异,特别是gems,可能会导致不一致的行为。在每个环境中,尝试

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

gem uninstall <gem-name>
gem install <gem-name> -v <desired-version-number>
gem卸载
gem安装-v

我很感激你在问这个问题上花了不少功夫,但仅仅根据表面的描述很难回答这个问题。这并不是说有一个“正确工作”按钮,每个人都知道你应该按,而你就是不按。这看起来像是我亲自动手解决的问题——拿出一个记录器/调试器,试着找出环境的不同之处。有几个显而易见的问题需要注意:系统上的gem版本和网络中的差异(因为这似乎是通过网络进行的)。哈!谢谢Redyaffle,成功了!较新的安装只是有一个gems的最新版本。卸载新的和安装“旧的”像一个冠军一样迅速到位。