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

**URL:** <https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304>\
**Category:** Ruby\
**Created:** [May 16, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304 "2007-05-16T16:51:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304/1 "2007-05-16T16:51:49Z")

</div>

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)  
…

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304/2 "2007-05-16T17:02:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304/3 "2007-05-16T17:04:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304/4 "2007-05-16T17:11:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2007, 7:19pm UTC](https://www.ruby-forum.com/t/why-doesnt-a-renamed-file-adhere-to-an-original-variable/104304/5 "2007-05-16T19:19:57Z")

</div>

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
