Why doesn't a renamed file adhere to an original variable?

P

Peter Bailey

I need to send some graphics file over to UNIX land, but, without an
extension. So, I rename the files and try to send them, and, Ruby
complains that the file isn't there. Why is it that a file rename
doesn't continue to be legitimate with the original variable name? When
it's time to "put" the file, Ruby says that the file doesn't exist, I
guess because it still think that the variable is pointing to the file
with the extension. What's the point of File.rename if the files can't
stick with the variable names?

Thanks a lot,
Peter


require 'net/ftp'
...

Dir.glob("*.100").each do |arborfile|
File.rename(arborfile, File.basename(arborfile, ".100"))
ftp.putbinaryfile(arborfile.downcase)
...
 
S

Stefano Crocco

Alle mercoled=C3=AC 16 maggio 2007, Peter Bailey ha scritto:
I need to send some graphics file over to UNIX land, but, without an
extension. So, I rename the files and try to send them, and, Ruby
complains that the file isn't there. Why is it that a file rename
doesn't continue to be legitimate with the original variable name? When
it's time to "put" the file, Ruby says that the file doesn't exist, I
guess because it still think that the variable is pointing to the file
with the extension. What's the point of File.rename if the files can't
stick with the variable names?

Thanks a lot,
Peter


require 'net/ftp'
...

Dir.glob("*.100").each do |arborfile|
File.rename(arborfile, File.basename(arborfile, ".100"))
ftp.putbinaryfile(arborfile.downcase)
...

arborfire is simply a string, it's not related with a file at all. File.ren=
ame=20
changes the name of the file on the filesystem. When you use=20
ftp.putbinaryfile, you pass it the old filename, which doesn't refer to a=20
file anymore, since you renamed it. What you want is this:

Dir.glob("*.100").each do |arborfile|
new_name =3D File.basename(arborfile, ".100")
File.rename arborfile, new_name
ftp.putbinaryfile(new_name.downcase)
=2E..

I hope this helps

Stefano
 
J

Jeremy Hinegardner

I need to send some graphics file over to UNIX land, but, without an
extension. So, I rename the files and try to send them, and, Ruby
complains that the file isn't there. Why is it that a file rename
doesn't continue to be legitimate with the original variable name? When
it's time to "put" the file, Ruby says that the file doesn't exist, I
guess because it still think that the variable is pointing to the file
with the extension. What's the point of File.rename if the files can't
stick with the variable names?

This is bit of a misconception. The variable "arborfile" in your
snippet is just a String and Strings are immutable. In this case
arborfile holds as a String the pathname of a file ending in .100.

The File.rename(old_name,new_name) class method takes 2 Strings,
representing a pathname to the 'old_name' of the on-disk file and the
'new_name' of the on-disk file. It then moves the on-disk file located
at at the path in 'old_name' and moves it to the path in 'new_name'. It
does not manipulate the String in 'old_name' or the String in
'new_name'.
require 'net/ftp'
...

Try:

Dir.glob("*.100").each do |arborfile|
new_name = File.basename(arborfile,".100")
File.rename(arborfile, new_name)
ftp.putbinaryfile(new_name.downcase)

Or let ftp put it with the new name:

Dir.glob("*.100").each do |arborfile|
ftp.putbinaryfile(arborfile,File.basename(arborfile,".100"))

enjoy,

-jeremy
 
P

Peter Bailey

Thank you, guys, both of you. Now I get it. It doesn't quite subscribe
to my primitive logic, I guess, but, I'll learn to live with it.
 
B

Brian Candler

This is bit of a misconception. The variable "arborfile" in your
snippet is just a String and Strings are immutable.

Errm?

irb(main):001:0> a = "foo"
=> "foo"
irb(main):002:0> a.object_id
=> -605382622
irb(main):003:0> a << "bar"
=> "foobar"
irb(main):004:0> a.object_id
=> -605382622
irb(main):005:0> a.replace("xxx")
=> "xxx"
irb(main):006:0> a.object_id
=> -605382622
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top