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

  • Thread starter Boris \BXS\ Schulz
  • Start date
B

Boris \BXS\ Schulz

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><NoMethodError>
from 1-to-01.rb:2:in 'foreach'
from 1-to-01.rb:2


greetings, BXS
 
T

ts

"B" == Boris \"BXS\" Schulz <Boris> writes:

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

this is a singleton method

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

File.rename(filename, newname)

B> end


Guy Decoux
 
B

Boris \BXS\ Schulz

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

greetings, BXS
 
T

ts

"B" == Boris \"BXS\" Schulz <Boris> writes:

B> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top