Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read contents of a local file into a variable in Rails

All I want to do is get all the content from a local file and store it in a variable. How?

File.read(@icon.full_filename).each {|l| r += l}

only gives me a part of it. In PHP, I just used file_get_contents.

like image 879
Steven Avatar asked Jun 16 '10 16:06

Steven


People also ask

How can you read an entire file and get each line in Ruby?

Use File#readlines to Read Lines of a File in Ruby Newline character \n may be included in each line. We must be cautious when working with a large file, File#readlines will read all lines at once and load them into memory.


1 Answers

data = File.read("/path/to/file") 
like image 139
zed_0xff Avatar answered Oct 17 '22 06:10

zed_0xff