Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 332 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write to file in Ruby?

#1
I need to read the data out of database and then save it in a text file.

How can I do that in Ruby? Is there any file management system in Ruby?
Reply

#2
Are you looking for the following?

File.open(yourfile, 'w') { |file| file.write("your text") }
Reply

#3
[Zambri][1]'s answer [found here][2] is the best.


File.open("out.txt", '<OPTION>') {|f| f.write("write your stuff here") }

where your options for `<OPTION>` are:

`r` - Read only. The file must exist.

`w` - Create an empty file for writing.

`a` - Append to a file.The file is created if it does not exist.


`r+` - Open a file for update both reading and writing. The file must exist.

`w+` - Create an empty file for both reading and writing.

`a+` - Open a file for reading and appending. The file is created if it does not exist.

In your case, `w` is preferable.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
For those of us that learn by example...

Write text to a file like this:

IO.write('/tmp/msg.txt', 'hi')

BONUS INFO ...

Read it back like this

IO.read('/tmp/msg.txt')

Frequently, I want to read a file into my clipboard ***

Clipboard.copy IO.read('/tmp/msg.txt')

And other times, I want to write what's in my clipboard to a file ***

IO.write('/tmp/msg.txt', Clipboard.paste)

*** Assumes you have the clipboard gem installed

See:

[To see links please register here]

Reply

#5
This is preferred approach in most cases:

File.open(yourfile, 'w') { |file| file.write("your text") }

When a block is passed to `File.open`, the File object will be automatically closed when the block terminates.

If you don't pass a block to `File.open`, you have to make sure that file is correctly closed and the content was written to file.

begin
file = File.open("/tmp/some_file", "w")
file.write("your text")
rescue IOError => e
#some error occur, dir not writable etc.
ensure
file.close unless file.nil?
end

You can find it in [documentation][1]:

static VALUE rb_io_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE io = rb_class_new_instance(argc, argv, klass);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}

[1]:

[To see links please register here]

Reply

#6
The Ruby [File class](

[To see links please register here]

) will give you the ins and outs of `::new` and `::open` but its parent, the [IO class](

[To see links please register here]

), gets into the depth of `#read` and `#write`.
Reply

#7
To destroy the previous contents of the file, then write a new string to the file:

<!-- language: lang-rb -->

open('myfile.txt', 'w') { |f| f << "some text or data structures..." }

To append to a file without overwriting its old contents:

<!-- language: lang-rb -->

open('myfile.txt', "a") { |f| f << 'I am appended string' }
Reply

#8
You can use the short version:

File.write('/path/to/file', 'Some glorious content')

It returns the length written; see [::write][1] for more details and options.

To append to the file, if it already exists, use:

File.write('/path/to/file', 'Some glorious content', mode: 'a')


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through