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

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)

Alle mercoledì 16 maggio 2007, Peter B. ha scritto:

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.rename
changes the name of the file on the filesystem. When you use
ftp.putbinaryfile, you pass it the old filename, which doesn’t refer to
a
file anymore, since you renamed it. What you want is this:

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

I hope this helps

Stefano

On Wed, May 16, 2007 at 11:51:50PM +0900, Peter B. wrote:

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

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.

On Thu, May 17, 2007 at 12:04:21AM +0900, Jeremy H. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs