Ruby beginner Having issue writing variables to user created .txt file

this is some thing I’m doing for my job, I figured I’d try it in ruby

instead of doing it in c++ with qt…

class Setup
attr_accessor :folder, :file
@folder.to_s
@file.to_s
end

class MakeListings
attr_accessor :website, :username, :password, :description
@website.to_s
@username.to_s
@password.to_s
@description.to_s
end

def Install()

@folder = gets.chomp
Dir.mkdir(@folder) unless Dir.exists?(@folder)
@file = gets.chomp
outfile=File.open(@file,“w+”)
outfile.close
Main();
end

def CreateWebsite()

wMessage_one = “\nPlease input the site for this listing”

puts wMessage_one

@website = gets.chomp

File.open(@file, “a+”)

file << “#{@website}”

file.close

CreateUsername();

end

#I would have indented but it kept cutting it off and making it look
#worse

Every thing works except for opening and writing to the file

the File path contains the absolute file path given by the user.

Example c:\Random Test

So the folder gets created

#and then c:\Random Test\Hello there.txt the text file is created

How can I open this text file and append it? It doesn’t seem to like

the #@file variable

Aside from the fact that the usage of the idea of sharing the @file
variable between two top-level functions, while technically correct, is
a bit odd, note that you are using inside your function CreateWebsite
two DIFFERENT variables with similar name: ‘@file’, which is an instance
variable (in this case, I think, injected into the instance of type
Object, because you are not within a class definition), and ‘file’,
which is a local variable within the function.

BTW, be a little bit more precise when describing a problem. When you
say “It doesn’t seem to like …”, this is not something which has a
deeper meaning. Does it crash? Does it throw an exception? Does it hang
forever?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs