File.open returns an integer, not file inconsistency

temp_image = File.open(Rails.root.join("tmp","project_image.png"), 'wb') do |f| f.write(Base64.decode64(image_data)) end puts temp_image puts File.open(Rails.root.join("tmp","project_image.png")) 

Output:

24018

<File: 0x007fdddd55c8db0>

I want the first line to also return a file object. Why does it return an integer?

+4
source share
1 answer

The operator x = File.open(...) do |f| ... x = File.open(...) do |f| ... sets x to the result of the do block, and not to the returned file (which closes after the block finishes). So what you are effectively doing is setting temp_image to the result of f.write , which is the number of bytes written, not the file.

+4
source

All Articles