class DobbsyKretts
def initialize
#Receive idea
puts "Enter an idea, a secret or anything else you want to secretize; hit enter to stop typing and save the file"
(@idea = gets).reverse.upcase
#Filename and saving - to encrypt the file
puts "Enter the file name you'd like to have this saved as; Type PLAN at the beginning for plans and REM for reminders"
(@file_name = gets.chomp.upcase)
File::open("DobbsyKrett-"+ @file_name + ".txt", "w") do |f|
f << @idea
end
end
def unzip
puts "Do you want to withdraw PLAN or REM"
response = gets.chomp.upcase!
puts "Invalid" if !["PLAN","REM"].include?(response)
file_contents = nil
Dir['DobbsyKrett-'+response+"*.txt"].each do |file_nom|
file_contents = File.read(file_nom)
end
puts file_contents
end
end
somethingsomething1 = DobbsyKretts.new
somethingsomething1.unzip
def unzip
puts "Do you want to withdraw PLAN or REM"
@response = gets.strip
if @response.downcase != "plan" and @response.downcase != "rem"
puts "Invalid" end
Dir["DobbsyKrett-"[email protected]+".txt"].each do |file_nom|
@value = file.read(file_nom)
end
puts @value
end
end
回答
函數gets
將在結尾處返回一個字符串,其行結束字符與您的預期不符。要刪除它,使用格格功能:
@response = gets.chomp
這是正常的方法(例如unzip
)來創建新的實例變量(例如@valueholder
)。一般來說它總是爲你的變量最好有儘可能小的範圍內,所以,除非你需要稍後閱讀valueholder
,你應該只使用一個局部變量(從名稱中刪除@
):
Dir["DobbsyKrett-"[email protected]+".txt"].each do |file_nom|
valueholder = File.read(file_nom)
end
puts valueholder
而且,valueholder
對於一個變量來說是一個可怕的名字,但是如果你把它變成了一個可以原諒的局部變量。
此外,您的塊啓動/結尾不匹配。這是你的函數的一個固定的版本,不應該導致語法錯誤:
def unzip
puts "Do you want to withdraw PLAN or REM"
response = gets.chomp.downcase
if !["plan","rem"].include? response
puts "Invalid"
else
Dir["DobbsyKrett-#{response}.txt"].each do |file_nom|
valueholder = file.read(file_nom)
end
puts valueholder
end
end
編輯:你應該利用File
能正確調用File.read
。
然後@response會發生什麼? Ruby如何知道您是指在DIR行中使用PLAN還是REM? – HareKrishna
對不起,我的錯誤...我會糾正我的回答 –
謝謝...代碼的最後一行(看我的,我更新)應該打印所有的DobbsyKrett-PLAN ...文件或所有的DobbsyKrett- REM ...文件。但是我只有在創造我的課程時才支持我創造的任何東西。爲什麼是這樣? – HareKrishna
- 1. 簡單的Ruby類混淆和調試
- 2. 調試ActionScript類文件最簡單的方法是什麼?
- 3. 爲什麼Ruby中沒有繼承這個簡單的測試類的方法?
- 4. 簡單的ruby方法幫手
- 5. 調用Ruby類方法
- 6. 什麼是調試Node.js的簡單任務最好的,簡單的方法
- 7. 在Ruby中的單例類中調用單例方法?
- 8. 單元測試調用另一個類的方法的方法
- 9. 從類方法調用變量的簡單PHP OOP語法
- 10. 編寫調試一個簡單的打印方法的Java
- 11. Ruby中的子類方法在父類中調用方法
- 12. Ruby:調用模塊中的類方法
- 13. Ruby中包含類的調用方法
- 14. 父類的Ruby調用子方法
- 15. 測試系統DSN的簡單方法?
- 16. 簡單的方法來測試AJAX Post?
- 17. 角度 - 調試時查看範圍的更簡單方法?
- 18. 簡單的方法來運行和調試Node.js項目?
- 19. 最簡單的方法就是開始調試PHP(Drupal)
- 20. 如何在jQuery中調試最簡單的方法?
- 21. 更簡單的調試方法Maven使用eclipse構建
- 22. 調用struts2動作的簡單方法
- 23. 簡單回調的最佳方法
- 24. 在java中調用簡單的方法
- 25. 從C++調用'貓'的簡單方法?
- 26. Ruby - 類方法
- 27. 簡單的創建類別的方法
- 28. 最簡單的寫/讀類的方法
- 29. Ruby初學者試圖從Ruby中的另一個類調用一個方法?
- 30. 簡單的調試查詢
http://sscce.org/ –