我想讓這個類初始化以接收要保存的消息併爲其輸入文件名。我是否正在繪製一個錯誤,因爲Ruby只需要在init方法中實例化值?溫柔,我是新手。 Traceback粘貼在下面。Ruby Class,我懷疑我正在做實例標記錯誤
class DobbsyKretts
idea = 'arbitaryvalue'
def initialize
#Receive idea
puts "Enter an idea, a secret or anything else you want to encrypt. 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.strip
File::open("DobbsyKrett-"+ file_name + ".txt", "w") do |f|
f>>@idea
end
end
end
something = DobbsyKretts.new
回溯:
testy.rb:11:in `initialize': private method `gets' called for nil:NilClass (NoMethodError)
from testy.rb:21:in `new'
from testy.rb:21:in `<main>'
Enter an idea, a secret or anything else you want to encrypt. Hit enter to stop typing and save the file
什麼是「實例標記」? –