Writing a file to a specific path in ruby taking the file name from excel - ruby-on-rails

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.

I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem

You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

Related

Command line args in F# fsx

I run my .fsx file like
>fsi A.fsx
In this file I read csv with CsvProvider that has to have path to csv data.
type Data = CsvProvider<"my_data.txt", ";", Schema
I need to pass file name as command line argument and it is possible
>fsi A.fsx my_data.txt
I can read it like
let originalPath = fsi.CommandLineArgs.ElementAt(1)
Problem is, that file name used in CsvProvider constructor needs to be constant and command line argument is not. How I can initialize CsvProvider from command line argument?
The value inside the angle brackes <"my_data.txt"...> specifies an example format file and is checked at compile time, hence the need for it to be a constant string. Assuming your .fsx script merely wants to load a different CSV file of the same general format, you would use
let contents = Data.Load(originalPath)

Not able to create a file for writing

I am uploading txt files using carrierwave. The files are not small (80 MB - 500 MB) and I want to remove some of the lines to reduce this size (about 80% of the file size is going to be reduced).
I have created a model method in order to clear these lines:
require 'fileutils'
def clear_unnecessary_lines
old_file_path = Rails.root.join('public').to_s + log_file.to_s
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
File.open(old_file_path, 'r') do |old_file|
File.open(new_file_path, 'w') do |new_file|
old_file.each_line do |line|
new_file.write(line) unless line.grep(/test/).size > 0
end
end
end
FileUtils.mv new_file_path, old_file_path
end
but I am getting error when I am trying to open the new file saying there is no such file. As I have read opening a file with the w option should create an empty file for writing. Then why I am getting such error?
Also, since log_file column is holding the path to the original file, and I am changing it, could you tell how to rename the new file with the old name? As I have checked I should specify only old and new names, not paths.
Errno::ENOENT: No such file or directory - /home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt
It is strange that If I execute the following command in rails console, it is not throwing an error and the file is created.
File.open('/home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt','w')
Ah, i see your problem now. When you do this
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
you call the "self-altering" version of sub, ie sub!. This will actually change the value of old_file_path as a side effect. Then, in the next line, you try to open this file, which hasn't been created yet. Take out the exclamation mark and you should be fine.

"latex-rails" gem generates no output

I have a problem with "latex-rails" gem. I`m trying to make function which will generate a pdf. This is my code:
code = "\\documentclass[12pt]{article}
\\begin{document}
Don't forget to include examples of topicalization.
\\end{document}"
#latex_config={:command => 'xelatex',:parse_twice => true}
LatexToPdf.generate_pdf(code, #latex_config, parse_twice = true)
In a log file I can see that "Output written on input.pdf (1 page).", but there is no input.pdf and I have no clue what is wrong.
For the sake of having an answer posted here instead of in the comments...
The LatexToPdf.generate_pdf method returns the pdf binary itself, which you'll need to write to a file. Here's one way to accomplish this:
code = "\\documentclass[12pt]{article} \\begin{document} Test \\end{document}"
latex_config = {command: 'xelatex', parse_runs: 2}
result = LatexToPdf.generate_pdf(code, latex_config)
f = File.new("testfile.pdf", "w")
f.write(result)
f.close
As you noted, the log file states that the output was written to a file; however, rails-latex writes this file to a temporary directory and destroys the directory at the end of the method (hence the need to write the returned binary content to a file yourself).

load file in lua

I have directory that contain multiple file
i need to load on specified file.
i know i can use loadfile(path) but how i need to specify which file to load
thank you
Jp
I'm not 100% sure I understand what you are asking, but here's my take on it. If you know the directory you need to load the file from, you'd just prefix it to the name of the file:
local f, error = loadfile(mydir .. "/my_file")
Note that this reads and parses the file. To actually execute it, you need to invoke the function you get back from loadfile() (so f() in this example). If there is an error, loadfile() returns nil and an error message.
path = "./path/to/a/file.lua"
local myreturn1, myreturn2 --[[etc]] = assert(loadfile(path))(myarg1, myarg2, myarg3 --[[etc]])

Rails creating local xml file

I need to create a local xml file from a rails application and then copy it to a location on another server.
I have tried using the File.new option to create a new file but it gives me an error saying the file does not exist. After looking closer at the documentation it says that File.new opens a file that already exists.
I can't see any way to create a local file using Ruby, what am I missing?
Assuming you have built up your XML into a string, xml_string, you can do:
xml_file = open(filename, 'w')
xml_file.write xml_string
xml_file.close
Or using the block syntax to achieve this in one line:
File.open(local_filename, 'w') { |f| f.write(xml_string) }

Resources