简体   繁体   中英

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)

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.

at the beginning of the program write the below given statement. As IO is a class, standing for InputOutput, containing all methods including open

require IO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM