Arun Arun - 1 month ago 9
Ruby Question

private method `open' called for #<File:arun1.txt> (NoMethodError)

class Arun
def arun
afile=File.new("arun1.txt")
if afile
afile.open("arun1.txt","w")
afile.syswrite("hi from arunkumar.............")
afile.close()
afile.open("arun1.txt","r+")
con=afile.sysread(30)

puts con
afile.close()
else
puts "can't open"
end

end
end
a=Arun.new
a.arun


Here I can't access the
arun
method its shows private method
open
called for
#<File:arun1.txt>
(NoMethodError)

Answer

Have a look at the Ruby documentstion on File IO

Specifically, in order to write to a file just use something like

File.open("arun.txt", 'w') {|f| f.write("hi from arunkumar.............") }

As your error indicates, the open method isn't available for an object.

Comments