Another String Manipulation Exercise


#1

Hi guys,

And I thought it wasn’t a problem. Here’s my string

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

How do i take out all the text that comes before “mypic.jpg”?

Trickier than I first thought.

TIA,

Bing


#2

On 5/30/06, Bing removed_email_address@domain.invalid wrote:

Hi guys,

And I thought it wasn’t a problem. Here’s my string

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

How do i take out all the text that comes before “mypic.jpg”?

File.basename("/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg")


#3

Jim C. wrote:

On Tue, May 30, 2006 at 05:39:55AM +0200, Bing wrote:

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”
How do i take out all the text that comes before “mypic.jpg”?

irb(main):001:0>
File.basename("/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg")
=> “mypic.jpg”

-jim

Sweet! Thanks so much jim!


#4

On Tue, May 30, 2006 at 05:39:55AM +0200, Bing wrote:

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”
How do i take out all the text that comes before “mypic.jpg”?

irb(main):001:0>
File.basename("/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg")
=> “mypic.jpg”

-jim


#5

On Tuesday, May 30, 2006, at 5:39 AM, Bing wrote:

TIA,

Bing


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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

a = “/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

a[/(\w+.\w+)$/] => ‘mypic.jpg’

_Kevin


#6

Does File.basename require that the file exists on the local file
system?


#7

Bing wrote:

Hi guys,

And I thought it wasn’t a problem. Here’s my string

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

How do i take out all the text that comes before “mypic.jpg”?

If you have

filename = “/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

then

filename.gsub(/.*//,’’)

does what you asked, but

File.basename(filename)

works too.

Ray


#8

On Tue, May 30, 2006 at 05:54:54AM +0200, Bing wrote:

Jim C. wrote:

On Tue, May 30, 2006 at 05:39:55AM +0200, Bing wrote:

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”
How do i take out all the text that comes before “mypic.jpg”?

File.basename("/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg")

Sweet! Thanks so much jim!

Thank Phillip, he got his reply in a minute or so before mine :slight_smile:

-jim


#9

On Mon, May 29, 2006 at 09:08:37PM -0700, Zack H. wrote:

Does File.basename require that the file exists on the local file system?

No, it just understands how to parse things that look like filenames :slight_smile:
However, those filenames must use / characters.
You can also remove suffixes (e.g. remove the .jpg extension)
http://www.ruby-doc.org/core/classes/File.html#M000034

-jim


#10

You could use the split method with a -1 array reference
eg.

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”.split("/")[-1]
=> “mypic.jpg”


#11

Zack H. wrote:

Does File.basename require that the file exists on the local file system?

No, I used your filename in irb and I don’t have that file on my system.
The filename is just a string in the particular format of your OS.

If your OS uses a different format for filenames, then the parsing will
be different. Example: on Mac OS X, VMS filenames aren’t correctly
parsed:

File.basename(“NODE::DEVICE:[.DIR.DIR2]FILE.EXT;VERS”)
=> “NODE::DEVICE:[.DIR.DIR2]FILE.EXT;VERS”

On VMS Ruby (if there is such a beast) I’d get

FILE.EXT; VERS for this filename and

“/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg”

for yours.

Ray


#12

I was initially using .gsub but couldn’t get it to work (this was before
I posted my question). I will try out gsub again, and the split method
and the one from Kevin…apparently there are more ways to skin a cat…

thanks so much for everyone’s help!

Bing