Write file

Is it possibile to create with ruby css file to write in
public/stylesheets?
Obviously the name of the css file is variable and the internal
formatting is almost the same.

Thanks!

Luigi wrote:

Is it possibile to create with ruby css file to write in
public/stylesheets?
Obviously the name of the css file is variable and the internal
formatting is almost the same.

Thanks!

File.open(pathname, mode) {block} :

so i would do something like:

filename = “name_of_css_file.css”
css_file = File.open(RAILS_ROOT + ‘public/stylesheets’ + filename, ‘w’ )
{ |f|
f.puts “#someid { display: block; }/n” }

for more info:
http://www.rubycentral.com/book/html/tut_io.html
check out the IO, File, FileUtils classes; they are what you are looking
for.

hope it helps,

s

But in my Web ruby app directory where can I write the command lines?

Thank you!

you could create an create_css.rb file, place it in the lib/ directory,
and call it into any place where you want to use this method.

so you could do something like:
(UNTESTED CODE)

***** lib/create_css.rb ******

module CreateCss

# btw, this statement (file.open) returns nil
def make_css(filename, css_content)
  File.open(RAILS_ROOT + 'public/stylesheets' + filename, 'w') { |f|
            f.puts css_content }

end

end

and then, where ever in the app you want to do this (you could probably
even create the css_content with some form, and retrive it as params, or
whatever) just call the make_css method, and pass it the correct
parameters.
you’ll need to include the module, however.
example:
(UNTESTED CODE)

require ‘create_css.rb’ # includes the file
class SomeArbitraryController
include CreateCss # includes the specific module


# here you want to create the file:

def someaction
    .... # do some stuff
    make_css("a_new_css_file", params[:content_of_file])
    ... # continue doing some stuff
end

end


and walla! you should get a new file in your public folder:

 public/stylesheets/a_new_css_file.css

and if you opened it and looked at the content, it would contain the
content of params[:content_of_file].

cheers,

–shai

On 6/27/07, Luigi [email protected] wrote:

Is it possibile to create with ruby css file to write in
public/stylesheets?
Obviously the name of the css file is variable and the internal
formatting is almost the same.

Google for “dry up your css” for some approaches to this kind of thing