--20cf30667e8339cbf104b558f2b2
Content-Type: text/plain; charset=UTF-8

On Sat, Dec 31, 2011 at 12:23 AM, Alex Sweps <alexszepes / gmail.com> wrote:

> Hello Everyone
>
> I am hoping someone can help me. I have just started learning ruby


Welcome :-)


> and i
> am in the process of creating a script that will rename files based on
> some of the file contents. So far i have got what i want but need the
> file to be renamed with a variable but cannot for the life of me find
> out how to simply rename the original input file. Below is an example of
> what i mean.
>
> x  ile.readlines(ARGV[0])
> random code...
> filename  filetobenamed"
>
> So now i want to rename the original input file with something like
> file.name("#{filename}")
>
> One idea i thought of was to just create a new file with the contents of
> x but even that was proving more difficult than i expected.
>
> Any ideas would be greatly appreciated?
> Iv searched around for hours and tried various methods but cannot find a
> solution.
>
> Thanks :)
>

I think I found it.

The "Pickaxed" Ruby 1.9 book  mentions File.rename as a class method
(page 504).

A little test:

peterv@ASUS:~$ vim original # just made a 1 line file
peterv@ASUS:~$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
peterv@ASUS:~$ irb
...
004:0> File.open("original") do |f|
005:1* puts f.readline
006:1> end
my file
nil
007:0> File.rename("original", "new_name")
0
008:0> File.open("new_name") do |f|
009:1* puts f.readline
010:1> end
my file
nil
011:0> quit
peterv@ASUS:~$ ls -l | grep new_ # so it is really renamed
-rw-r--r--  1 peterv peterv     8 2011-12-31 01:56 new_name

Actually, a Google search for "ruby file rename",
brings me to

http://ruby-doc.org/core-1.9.3/File.html#method-c-rename

Good luck with Ruby :-)

Peter

-- 
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com

--20cf30667e8339cbf104b558f2b2--