Hi all,
My goal is to obtain the filename out of a full pathname.
This is the code:
fullname = "./Dir/file.txt"
array = fullname.split( /.*\// ) # Searches greedyly until the last
slash
The results are:
puts array.length ==> 2
puts array[0] ==> (newline symbol or something??)
puts array[1] ==> file.txt
My questions are:
Why has the array a length of 2?
What is in array[0]?
Best regards,
Francis
Because you're splitting, not matching.
./Dir/file.txt
^^^^^^ <-- What's here is your separator.
There's nothing *before* your separator, so array[0] is an empty
string; array[1] is the filename. There are two ways that are
better:
fullname = "./Dir/file.txt"
# Alternate regex form to avoid escaping.
array = fullname.split(%r{/}, -1)
puts array.length, array[-1]
# Best way
puts File.basename(fullname)
-austin
ยทยทยท
On 7/28/05, francisrammeloo@hotmail.com <francisrammeloo@hotmail.com> wrote:
My goal is to obtain the filename out of a full pathname.
This is the code:
fullname = "./Dir/file.txt"
# Searches greedyly until the last slash
array = fullname.split(/.*\//)
The results are:
puts array.length ==> 2
puts array[0] ==> (newline symbol or something??)
puts array[1] ==> file.txt
My questions are:
Why has the array a length of 2?
What is in array[0]?
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca