Forum: Ruby on Rails Another String Manipulation Exercise

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bing (Guest)
on 2006-05-30 07:39
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
Phillip H. (Guest)
on 2006-05-30 07:49
(Received via mailing list)
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")
Jim C. (Guest)
on 2006-05-30 07:52
(Received via mailing list)
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
Bing (Guest)
on 2006-05-30 07:54
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!
Kevin O. (Guest)
on 2006-05-30 07:58
(Received via mailing list)
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
Ray B. (Guest)
on 2006-05-30 08:04
(Received via mailing list)
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
Zack H. (Guest)
on 2006-05-30 08:10
(Received via mailing list)
Does File.basename require that the file exists on the local file
system?
Jim C. (Guest)
on 2006-05-30 08:32
(Received via mailing list)
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 :-)
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
Jim C. (Guest)
on 2006-05-30 08:32
(Received via mailing list)
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 :-)

-jim
Ray B. (Guest)
on 2006-05-30 08:41
(Received via mailing list)
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
Daniel -. (Guest)
on 2006-05-30 08:44
(Received via mailing list)
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"
>>
Bing (Guest)
on 2006-05-30 09:18
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
This topic is locked and can not be replied to.