File basenames using File.basename .

Oh, you're just seeing what irb returns at the end of Array#each.

Try this:

txtfiles.each{ |txtfile|
   p File.basename(txtfile, '.txt')
}

If you want to change the results of the array inline, do this:

txtfiles.map!{ |txtfile| File.basename(txtfile '.txt') }

Regards,

Dan

···

-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Peter Bailey
Sent: Wednesday, April 12, 2006 1:53 PM
To: ruby-talk ML
Subject: Re: File basenames using File.basename . . .

Berger, Daniel wrote:
>> a different extension for a separate write file. I've tried this:
>>
>> txtfiles = Dir.glob("*.txt")
>> txtfiles.each { |txtfile| File.basename("txtfile", "*.txt") }
>
> Remove the quotes around txtfile.
> Change "*.txt" to just ".txt".
>
> Regards,
>
> Dan

Thanks, Dan. I still get the same thing, though:
irb(main):008:0> txtfiles.each { |txtfile| File.basename(txtfile,
".txt") }

=> ["eula.txt", "libcurl.COPYING.txt", "openssl.LICENSE.txt",
"psout2.txt", "run
dll32.exe.Z-missing.txt"]

This gets the same result, too:

irb(main):012:0> txtfiles.each { |txtfile|
File.basename(txtfile, ".*")
}

=> ["eula.txt", "libcurl.COPYING.txt", "openssl.LICENSE.txt",
"psout2.txt", "run
dll32.exe.Z-missing.txt"]

Berger, Daniel wrote:

>>
Thanks, Dan. I still get the same thing, though:
File.basename(txtfile, ".*")
}

=> ["eula.txt", "libcurl.COPYING.txt", "openssl.LICENSE.txt",
"psout2.txt", "run
dll32.exe.Z-missing.txt"]

Oh, you're just seeing what irb returns at the end of Array#each.

Try this:

txtfiles.each{ |txtfile|
   p File.basename(txtfile, '.txt')
}

If you want to change the results of the array inline, do this:

txtfiles.map!{ |txtfile| File.basename(txtfile '.txt') }

Regards,

Dan

Thanks, Dan. Both of your suggestions worked for me. Now, how could I
fulfill my instructions in the filesystem itself, not just in the array?
I know I can do it with a system("ren *.txt *."), but, how would I do it
in RUBY?

···

--
Posted via http://www.ruby-forum.com/\.