instance method file

Ruby on Rails 2.3.18

Since v2.3.18 Last seen in v2.3.18

Signature

file(filename, data = nil, log_action = true, &block)

Create a new file in the Rails project folder. Specify the relative path from RAILS_ROOT. Data is the return value of a block or a data string.

Examples

file("lib/fun_party.rb") do
  hostname = ask("What is the virtual hostname I should use?")
  "vhost.name = #{hostname}"
end

file("config/apach.conf", "your apache config")

Parameters

filename req
data opt = nil
log_action opt = true
block block
Source
# File railties/lib/rails_generator/generators/applications/app/template_runner.rb, line 45
    def file(filename, data = nil, log_action = true, &block)
      log 'file', filename if log_action
      dir, file = [File.dirname(filename), File.basename(filename)]

      inside(dir) do
        File.open(file, "w") do |f|
          if block_given?
            f.write(block.call)
          else
            f.write(data)
          end
        end
      end
    end

Defined in railties/lib/rails_generator/generators/applications/app/template_runner.rb line 45 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Rails::TemplateRunner

Type at least 2 characters to search.

↑↓ navigate · open · esc close