Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

add extension in ruby tempfile object

To add extension to a Ruby tempfile, I figured that tempfile module needs to be modified a little.

Make a class like this:

require 'tempfile'

class CustomTempfle < Tempfile
  def initialize(filename, temp_dir = nil)
    temp_dir ||= Dir.tmpdir
    extension = File.extname(filename)
    basename = File.basename(filename, extension)
    super([basename, extension], temp_dir)
  end
end
And then you can call the class and provide the filename with the extensio and write to the file like so.

CustomTempfle.open('filename.pdf', nil) do |tmp|
  File.open(tmp, 'wb') { |f| f << 'content_of_the_file' }
end
Comment

PREVIOUS NEXT
Code Example
Ruby :: rails group every 10 items in array 
Ruby :: In Jekyll - get the parent url path of a collection 
Ruby :: rails list down attributes of model 
Ruby :: rails loop 
Ruby :: ruby read file line by line 
Ruby :: ruby-on-rails 
R :: linetype ggplot in r 
R :: remove package in r 
R :: check type of column in r 
R :: How to Export a DataFrame to Excel File in R 
R :: make a sequence of timeseries per day in r 
R :: r mean by group 
R :: r dataframe column factor 
R :: rename variables in r 
R :: expression in r 
R :: round multiple columns in r 
R :: how to read in txt/csv files into r 
R :: repeat each value in a vector in r 
R :: how to make matrix in r 
R :: how to link world bank data into r 
R :: stat_poly_eq position 
R :: how to format a number in r 
R :: unset par mar 
R :: r select columns by vector of names 
R :: R difference | and || 
R :: dotted y intercept line in ggplot 
R :: ISO 2 letter country code in r 
Rust :: random number generator in rust 
Rust :: rust timestamp 
Rust :: rust import file 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =