2015-01-04 101 views
-2

我試圖編程ruby時出錯。 錯誤:C:/用戶/ PC華碩/桌面/ g.rb:3:<main>': undefined local variable or method 'y' for main:Object (NameError)紅寶石錯誤:未定義的本地變量或方法

這裏是我的代碼:

puts " Do you like to install hacking pack?" 
insta_one = gets.chomp 

if insta_one == y 
make 
else 
puts "Ok. Bye!" 
end 

def make 
awe = file.new("shell.bat","w") 
readme.puts("@echo off") 
readme.puts("color a") 
readme.puts("echo Installing hacking pack") 
readme.puts("Thanks for downloading rootShell!") 
readme.puts("My email - [email protected]") 
end 
+1

你比較''ìnsta_one'''到unexistant'' 'y'''變量。你想要的是:'''如果insta_one =='y'''''。 – romainsalles 2015-01-04 12:22:23

回答

0

之前,你可以使用一個方法,你需要定義它。在你的代碼,make是之後定義您嘗試調用它,所以紅寶石是不熟悉它,並拋出一個錯誤:

def make 
    File.open("shell.bat", 'w') do |readme| 
    readme.puts("@echo off") 
    readme.puts("color a") 
    readme.puts("echo Installing hacking pack") 
    readme.puts("Thanks for downloading rootShell!") 
    readme.puts("My email - [email protected]") 
    end 
end 

puts " Do you like to install hacking pack?" 
insta_one = gets.chomp 

if insta_one == 'y' 
    make 
else 
    puts "Ok. Bye!" 
end 
1

y不是字符串,但未定義的變量。

請更改線路3:

if insta_one == 'y' 
+0

謝謝,但現在它給了我一個錯誤,'make'是undefined – 2015-01-04 12:58:30

+0

'make'是方法。定義調用函數之前的方法。在'make'方法中,請將'file.new(...'更改爲'File.new(...'。因爲'File'是類,所以應該使用調用類方法的大寫。 – itochan 2015-01-04 13:16:29

+0

它仍然給我錯誤 – 2015-01-04 13:31:20

相關問題