# Write file

**URL:** <https://www.ruby-forum.com/t/write-file/171135>\
**Category:** Rails\
**Created:** [June 27, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/write-file/171135 "2007-06-27T18:23:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luigi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/73ab20/32.png) [@luigi](https://www.ruby-forum.com/u/luigi)\
**Post date:** [June 27, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/write-file/171135/1 "2007-06-27T18:23:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![luigi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/73ab20/32.png) [@luigi](https://www.ruby-forum.com/u/luigi)\
**Post date:** [June 28, 2007, 10:25am UTC](https://www.ruby-forum.com/t/write-file/171135/2 "2007-06-28T10:25:22Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![luigi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/73ab20/32.png) [@luigi](https://www.ruby-forum.com/u/luigi)\
**Post date:** [June 28, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/write-file/171135/3 "2007-06-28T13:59:43Z")

</div>

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

Thank you!

---

<div class="post-metadata">

**Author:** ![luigi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/73ab20/32.png) [@luigi](https://www.ruby-forum.com/u/luigi)\
**Post date:** [June 28, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/write-file/171135/4 "2007-06-28T15:40:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luigi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/73ab20/32.png) [@luigi](https://www.ruby-forum.com/u/luigi)\
**Post date:** [June 28, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/write-file/171135/5 "2007-06-28T16:30:42Z")

</div>

On 6/27/07, Luigi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
