Nuby question: f.rename(x,y) does not work

Hi,

somehow File.rename does not work in the little script i pasted below,
can someone help me out there ? I am using Windows XP Prof, and Ruby 180-10

···

#################################################################

regex = Regexp.new(’((1|2|3|4|5|6|7|8|9))’)
Dir::foreach(Dir.getwd){ |filename|
n = regex=~filename
if n
newname = filename.sub(regex, “(0#{filename[n+1].chr})”)
f = File.open(filename, “wb+”)
f.rename(filename, newname)
f.close()

end
}

#################################################################
the error i get is this:
undefined method `rename’ for #<File:Until Further Notice (1) Wooden
Man.mp3>
from 1-to-01.rb:2:in 'foreach’
from 1-to-01.rb:2

greetings, BXS

somehow File.rename does not work in the little script i pasted below,

           ^^^^^^^^^^^

this is a singleton method

    f = File.open(filename, "wb+")
    f.rename(filename, newname)
    f.close()

                File.rename(filename, newname)
      

  end

Guy Decoux

OK, so what do I do now ?
“Ruby In A Nutshell” says: Methods can be defined that are associated
with specific o jects only. Such methods are called singleton methods.
This does not really help me though :frowning:

greetings, BXS

OK, so what do I do now ?

Just do like in [ruby-talk:89181] use File.rename

svg% ri File::rename
----------------------------------------------------------- File::rename
     File.rename(old_name, new_name) => 0

···

------------------------------------------------------------------------
     Renames the given file to the new name. Raises a +SystemCallError+
     if the file cannot be renamed.

        File.rename("afile", "afile.bak") #=> 0

svg%

svg% ls b.rb c.rb
ls: c.rb: No such file or directory
b.rb*
svg%

svg% ruby -e 'File.rename("b.rb", "c.rb")'
svg%

svg% ls b.rb c.rb
ls: b.rb: No such file or directory
c.rb*
svg%

Guy Decoux

Ah, OK,

I understand now. Thanks a lot.

greetings, BXS