How to write a file in specific path in ruby -


i want save files in specific path.. have used this

file_name = gets  f = open.(dir.pwd, /data/folder /#{@file_name },w+) 

i'm not sure whether above line correct or not! dir.pwd tell directory path followed folder path , file name given.

it should store value on specific path specific file name given. can tell me how that.

your code has multiple errors. have ever tried execute script?

your script ends with:

test.rb:7: unknown regexp options - fldr test.rb:7: syntax error, unexpected end-of-input     f = open.(dir.pwd, /data/folder /#{@file_name },w+) 

first: need define strings ' or ":

file_name = gets  f = open.(dir.pwd, "/data/folder/#{@file_name}","w+") 

some other errors:

  • you use file_name , later @file_name.
  • the open method belongs file , needs 2 parameters.
  • the file defined constant f. use variable.
  • the path must concatenated. i'd use file.join it.
  • you don't close file.

after these changes get:

file_name = gets f = file.open(file.join(dir.pwd, "/data/folder/#{file_name}"),"w+") ## f.close 

and error:

  test.rb:29:in `initialize': no such file or directory @ rb_sysopen - c:/temp/data/folder/sdssd (errno::enoent) 

the folder must exist, must create first.

now script looks like:

require 'fileutils' dirname = "data/folder" file_name = gets.strip fileutils.mkdir_p(dirname) unless dir.exists?(dirname) f = file.open(file.join(dir.pwd, dirname, file_name),"w+") ##fill content f.close 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -