Q:

code refactoring using pathname and writing to a file

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

A:

How often are you going to be changing the mode? If not very often, I'd put it directly in the method, and do the rest like so:
def self.write_to_file(data, *args)
  file = Rails.root.join(*args)

  FileUtils.mkdir_p(file.dirname)
  File.open(file, "a+") { |f| f.puts(data) }
end

