I am using ruby on rails. Below given code works. However I was wondering if it can be written better.
# Usage: write 'hello world' to tmp/hello.txt file
# Util.write_to_file('hello world', 'a+', 'tmp', 'hello.txt')
def self.write_to_file(data, mode, *args)
input = args
filename = input.pop
dir = Rails.root.join(*input).cleanpath.to_s
FileUtils.mkdir_p(dir)
file = File.join(dir, filename)
File.open(file, mode) {|f| f.puts(data) }
end