How to Create This File Input and Output Assignment in Ruby

How do I create this File Input and Output assignment in Ruby

Write your code like:

File.open("exercise.txt", "r") do |fi|
file_content = fi.read

puts "This is an exercise log. It keeps track of the number hours of exercise."

hours = gets.chomp.to_f

end

Ruby's File.open takes a block. When that block exits File will automatically close the file. Don't use the non-block form unless you are absolutely positive you know why you should do it another way.

chomp the value you get from gets. This is because gets won't return until it sees a trailing END-OF-LINE, which is usually a "\n" on Mac OS and *nix, or "\r\n" on Windows. Failing to remove that with chomp is the cause of much weeping and gnashing of teeth in unaware developers.

The rest of the program is left for you to figure out.

The code will fail if "exercise.txt" doesn't already exist. You need to figure out how to deal with that.

Using read is bad form unless you are absolutely positive the file will always fit in memory because the entire file will be read at once. Once it is in memory, it will be one big string of data so you'll have to figure out how to break it into an array so you can iterate it. There are better ways to handle reading than read so I'd study the IO class, plus read what you can find on Stack Overflow. Hint: Don't slurp your files.

Ruby - How to write a new file with output from script

Outputting to a new file can be done like this (don't forget the second parameter):

output = File.open( "outputfile.yml","w" )
output << "This is going to the output file"
output.close

So in your example, you could do this :

File.open("us_cities.yml", "r+") do |file|
while line = file.gets
"do find a replace"
end
output = File.open( "outputfile.yml", "w" )
output << "Here I am writing to a new file"
output.close
end

If you want to append to the file, make sure you put the opening of the output file outside of the loop.

Assign text from file to variable in Ruby without returning text

Start your irb as below :

irb --simple-prompt --noecho

  • --simple-prompt is to get the IRB prompt as >>
  • --noecho is to off the echo on IRB

Here is an example:

C:\>irb --simple-prompt
>> x = 2
=> 2
>> exit

C:\>irb --simple-prompt --noecho
>> x = 2
>>

Creating a ruby file from CSV data?

My reputation is not high enough to ask you more questions about your requirements via comments, but here are a few facts that might help.

I am assuming that you have populated a Rails model with the information you need. I have no sample code from you so I'll provide an example with a 'Book' model. The Book model has a title and an author, both of type String.

target = "file.txt"
contents = ""

# Get all books as an array. Arrays have iterators so we can use 'each'
Book.all.each do |book|
# get the book title, append it to the string. Add a newline
contents.concat(book.title).concat($/)

# get the book author
contents.concat(book.author).concat($/)

# Just a line separator
contents.concat("-----").concat($/)
end
File.open(target, "w").write(contents)

The above would yield something like this inside the text file:

Book 1
Author 1
-----
Book 2
Author 1
-----
...

Reading up more on the Rails active records might help you as well: ActiveRecord

Ruby on Rails, form_for, paperclip, and mass-assignment of protected parameters

You need to update your database migration. Run:

rails g migration AddIdToAsset processmodel_id:integer
rake db::migrate

Creating a file for each URL of a site using Ruby

File.open("#{node_id}.txt", "w") do |f|
f.puts "stuff"
end

How you make the assignment to node_id is up to you.



Related Topics



Leave a reply



Submit