Expanding tildes in filenames


(Berger, Daniel) #1

Hi all,

Just curious if there’s a Ruby idiom out there for automatic tilde expansion
in filenames. Perl does it like this (taken from the Perl Cookbook, p.
231):

$filename =~ s/{ ^ ~ ( [^/]* ) }
{ $1
? (getpwnam($1))[7]
: ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7])
}ex;

Will I have to do something similar for Ruby?

How do people feel about adding automatic tilde expansion into the IO class?
In other words, you would be able to do:

IO.foreach("~/djberge/.vimrc"){ |line|
puts line
}

and it would automatically expand that to your home dir and read your .vimrc
file (for example).

Comments? Suggestions?

Thanks.

Dan


(ts) #2

IO.foreach("~/djberge/.vimrc"){ |line|

   IO.foreach(File::expand_path("~/djberge/.vimrc")) { |line|

   puts line
}

Guy Decoux