2017-04-02 99 views
-2

我修改了我的帖子,並設法在此處複製/粘貼代碼。我知道這很醜,但真的希望它能幫助找到錯誤。 錯誤消息是:「./v_c:2:in‘:未定義的局部變量或方法’主:對象(NameError)」./count:78:語法錯誤,意外的關鍵字結束,期待輸入結束

#!/usr/bin/env ruby 
  
# 
# This program is used for collecting web server visit information. 
# 
# Author: A. Genius 
# 
  
require 'optparse' 
  
def print_usage 
    puts "USAGE: v_c -d DNS_NAME" 
    exit 
end 


  
# add option switch and handler 
  
options = {} 
  
option_parser = OptionParser.new do |opts| 
    
# DNS_NAME argument 
 options[:dns_name] = nil 
    opts.on('-d', '--dns-name DNS_NAME', 'Specify a DNS NAME') { |dns_name| options[:dns_name] = dns_name } 
    
# HELP argument 
    options[:help] = nil 
    opts.on('-h', '--help', 'Display usage') { |help| options[:help] = help } 
end 
  
option_parser.parse! 
  
# verify arguments 
  
if options[:dns_name] then 
    dns_name = options[:dns_name] 
else 
  puts "Please set a balancer's DNS." 
  print_usage 
  exit 
end 
  
if options[:help] then 
    print_usage 
    exit 
end 
  
# Keep STDOUT 
# orig_stdout = $stdout 
  
# redirect stdout to /dev/null 
# $stdout = File.new('/dev/null', 'w') 
  
    server1_visit_count = 0 
    server2_visit_count = 0 
    server3_visit_count = 0 
    server4_visit_count = 0 
  
# starting to visit load balancing server 
  
puts "Starting to visit load balancing server" 

2000.times do 

# visit load balancer 
# o = "curl #{dns_name}" 

o = "curl -s #{dns_name}" 
if o =~ /server\s*1/i 
    server1_visit_count += 1 
elsif o =~ /server\s*2/i 
    server2_visit_count += 1 
elsif o =~ /server\s*3/i 
    server3_visit_count += 1 
elsif o =~ /server\s*4/i 
    server4_visit_count += 1 
end 

    print "." 
end 

puts 
puts '-------------------------' 
puts ' Summary' 
puts '-------------------------' 
puts "Server1 visit counts : " + server1_visit_count.to_s 
puts "Server2 visit counts : " + server2_visit_count.to_s 
puts "Server3 visit counts : " + server3_visit_count.to_s 
puts "Server4 visit counts : " + server4_visit_count.to_s 
puts "total is: " + (server1_visit_count + server2_visit_count + 

server3_visit_count + server4_visit_count).to_s 
+0

'elsif'是單個關鍵字,裏面沒有空格。另外,請發佈代碼,而不是截圖。 – mudasobwa

+0

你在這行'puts'得到的錯誤總訪問次數:「+(server1_visit_count + server2_visit_count + server3_visit_count + server4_visit_count).to_s'? –

+0

您可以將代碼複製並粘貼到問題中嗎?如果我能夠將代碼粘貼到我最喜歡的編輯器中並運行它,我會更容易幫助您。 – spickermann

回答

1

哦......串插,再次...更換此線 #o = 'curl #{dns_name}'有:

#o = "curl #{dns_name}" 

我解釋了爲什麼這是下面的問題。

  1. 從編輯器中可以看出end未被識別。也許你之前複製/粘貼了一些代碼,並且某些字符(如引號)與編輯器使用的格式不同。所以重寫這段簡短的和平代碼可能會節省您的###。並粘貼什麼這裏改寫:)

  2. elsif寫爲單個的詞,但紅寶石大概的理解,因爲沒有錯誤我得到了使用它這樣..

  3. 字符串插值使用雙引號

'curl -s #{dns}'

原本應該:

"curl -s #{dns}"

(期待你真的想輸出這是「curl -s #{dns}」)

是問題的其中之一?


我剛把這個粘貼到repl.it中,效果很好。請檢查它是否符合你的代碼(我已經增值到dns_name變量,因爲我沒有在我的情況下定義的那樣):

dns_name = 10 
10.times do 

# visit load balancer 
#o = "curl #{dns_name}" 

o = "curl -s #{dns_name}" 
if o =~ /server\s*1/i 
    server1_visit_count += 1 
elsif o =~ /server\s*2/i 
    server2_visit_count += 1 
elsif o =~ /server\s*3/i 
    server3_visit_count += 1 
elsif o =~ /server\s*4/i 
    server4_visit_count += 1 
end 

print "." 
end 

嘗試複製/粘貼,如果任何錯誤告訴:

#!/usr/bin/env ruby 
  
# 
# This program is used for collecting web server visit information. 
# 
# Author: A. Genius 
# 
  
require 'optparse' 
  
def print_usage 
    puts "USAGE: v_c -d DNS_NAME" 
    exit 
end 


  
# add option switch and handler 
  
options = {} 
  
option_parser = OptionParser.new do |opts| 
    
# DNS_NAME argument 
 options[:dns_name] = nil 
    opts.on('-d', '--dns-name DNS_NAME', 'Specify a DNS NAME') { |dns_name| options[:dns_name] = dns_name } 
    
# HELP argument 
    options[:help] = nil 
    opts.on('-h', '--help', 'Display usage') { |help| options[:help] = help } 
end 
  
option_parser.parse! 
  
# verify arguments 
  
if options[:dns_name] then 
    dns_name = options[:dns_name] 
else 
  puts "Please set a balancer's DNS." 
  print_usage 
  exit 
end 
  
if options[:help] then 
    print_usage 
    exit 
end 
  
# Keep STDOUT 
# orig_stdout = $stdout 
  
# redirect stdout to /dev/null 
# $stdout = File.new('/dev/null', 'w') 
  
    server1_visit_count = 0 
    server2_visit_count = 0 
    server3_visit_count = 0 
    server4_visit_count = 0 
  
# starting to visit load balancing server 
  
puts "Starting to visit load balancing server" 

2000.times do 

# visit load balancer 
# o = "curl #{dns_name}" 

o = "curl -s #{dns_name}" 
if o =~ /server\s*1/i 
    server1_visit_count += 1 
elsif o =~ /server\s*2/i 
    server2_visit_count += 1 
elsif o =~ /server\s*3/i 
    server3_visit_count += 1 
elsif o =~ /server\s*4/i 
    server4_visit_count += 1 
end 

    print "." 
end 

puts 
puts '-------------------------' 
puts ' Summary' 
puts '-------------------------' 
puts "Server1 visit counts : " + server1_visit_count.to_s 
puts "Server2 visit counts : " + server2_visit_count.to_s 
puts "Server3 visit counts : " + server3_visit_count.to_s 
puts "Server4 visit counts : " + server4_visit_count.to_s 
puts "total is: " + (server1_visit_count + server2_visit_count + server3_visit_count + server4_visit_count).to_s 
+0

它不符合這個條件'如果o =〜/ server \ s * 1/i'以及其他elsif的 –

相關問題