[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to Create a New File

Skeets

7/30/2006 5:34:00 AM

let's say I want to create a new file named new_file.txt. how do i do
this in ruby? it would seem to be trivial, however, i'm going through
the pickaxe book and i can't find anything that relates to this.

tia...

8 Answers

Harold Hausman

7/30/2006 5:44:00 AM

0

How about something like this:

File.open( 'new_file.txt', 'w' ) {|file| file.write( 'yo' )}

#hth,
# -Harold

On 7/30/06, Skeets <skillet3232@yahoo.com> wrote:
> let's say I want to create a new file named new_file.txt. how do i do
> this in ruby? it would seem to be trivial, however, i'm going through
> the pickaxe book and i can't find anything that relates to this.
>
> tia...
>
>
>

S Wayne

7/30/2006 5:45:00 AM

0

Different ways:

Easy pure ruby:

f = File.open("new_file.txt","w")
f.close

Or if you are on *nix/cygwin:

system("touch new_file.txt")

Morton Goldberg

7/30/2006 6:32:00 AM

0

This is just golfing, and I should be ashamed of myself, but
something like ...

`>~/new_file.txt`

... will do it on most Unix systems (including Mac OS X).

Regards, Morton

On Jul 30, 2006, at 1:35 AM, Skeets wrote:

> let's say I want to create a new file named new_file.txt. how do i do
> this in ruby? it would seem to be trivial, however, i'm going through
> the pickaxe book and i can't find anything that relates to this.

Skeets

7/30/2006 6:44:00 AM

0

thanks, everyone.

Harold's solution works. i tried this before posting, but got an
error. i guess i mucked up the code, b/c it works fine now.

thanks again.

Harold Hausman wrote:
> How about something like this:
>
> File.open( 'new_file.txt', 'w' ) {|file| file.write( 'yo' )}
>
> #hth,
> # -Harold
>
> On 7/30/06, Skeets <skillet3232@yahoo.com> wrote:
> > let's say I want to create a new file named new_file.txt. how do i do
> > this in ruby? it would seem to be trivial, however, i'm going through
> > the pickaxe book and i can't find anything that relates to this.
> >
> > tia...
> >
> >
> >

Skeets

7/30/2006 6:45:00 AM

0


Skeets wrote:
> let's say I want to create a new file named new_file.txt. how do i do
> this in ruby? it would seem to be trivial, however, i'm going through
> the pickaxe book and i can't find anything that relates to this.
>
> tia...

i know why my initial attempt was mucked up...

f = File.open( 'new_file.txt', 'w' )

i tried to assign it to a variable - that's a no no in this case.

Chad Perrin

7/30/2006 6:46:00 AM

0

On Sun, Jul 30, 2006 at 03:31:30PM +0900, Morton Goldberg wrote:
> This is just golfing, and I should be ashamed of myself, but
> something like ...
>
> `>~/new_file.txt`

Whee, CLI obfu. Well, something potentially useful for it, anyway.


>
> ... will do it on most Unix systems (including Mac OS X).

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

Hal E. Fulton

7/30/2006 7:09:00 AM

0

Skeets wrote:
> i know why my initial attempt was mucked up...
>
> f = File.open( 'new_file.txt', 'w' )
>
> i tried to assign it to a variable - that's a no no in this case.

Um, no... that's not a nono. :)

What did this do for you?

Of course, properly you'd use File.new in that case,
and if you use File.open you'd use a block...


Hal

SaPeIsMa

4/5/2013 11:24:00 AM

0

"jane" <jane.playne@gmail.com> wrote in message
news:338780dc-367a-43d8-9dca-e4481dbac8bd@n7g2000yqk.googlegroups.com...
On Apr 4, 8:54 am, "Ramon F. Herrera" <ra...@conexus.net> wrote:
> (a) The role of a lifeguard is not only to swim like a Johnny
> Weissmuller: They are also the authority figure, the cops.
>
> (b) While they are swimmer-in-training, they can take care of of the
> kiddie pool.
>
> (c) How good are they at yelling and throwing a life preserver?
>
> (d) How long does it take for a person to learn how to swim? (a lot
> less than learning a language).
>
#
# It actually takes a long time to become a strong swimmer to the point
# of being able to save a person without that panicked person grabbing
# you, pulling you under while trying to climb on top of you, and
# drowning you.
#
# I agree that in most pool situations, the lifeguard only has to throw
# a life preserver or hand the end of a pole to the person. However,
# once the situation gets past a certain point, getting in the water is
# the only option. At that point the person is so panic stricken that
# he WILL try grabbing you and will try to climb on top of you in an
# attempt to get his own head above water.
#
# Being a life guard not only requires being a strong swimmer, but also
# being trained in avoiding the situation described above AND being
# trained in how to recover from the above situation if the panicked
# individual does force you under.


Having been a lifeguard and lifeguard instructor, the 2 major aspects of
training are swimming and water survival skills.
Resuscitation comes in third.
And the simple reason for that is that unless you are able to get to the
victim, and survive their panicked attempts to climb on top of you, and then
have the strength and skill to swim back to shore with them, there will be 2
drownings instead of 1...