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.
5c26728d2db67a78f9097f63ed3c8d37?d=identicon&s=25 Bing (Guest)
on 2006-05-30 05: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
B1102f65359ee629df508c7857f03b1c?d=identicon&s=25 Phillip Hutchings (Guest)
on 2006-05-30 05:49
(Received via mailing list)
On 5/30/06, Bing <sombreroisland@gmail.com> 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")
685df76ebeb6c0d61ff5e0284830f691?d=identicon&s=25 Jim Cheetham (Guest)
on 2006-05-30 05: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
5c26728d2db67a78f9097f63ed3c8d37?d=identicon&s=25 Bing (Guest)
on 2006-05-30 05:54
Jim Cheetham 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!
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-30 05: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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails

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

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

_Kevin
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-05-30 06: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
E10a35d5f2a85c5f65fa848eaac48859?d=identicon&s=25 Zack Ham (Guest)
on 2006-05-30 06:10
(Received via mailing list)
Does File.basename require that the file exists on the local file
system?
685df76ebeb6c0d61ff5e0284830f691?d=identicon&s=25 Jim Cheetham (Guest)
on 2006-05-30 06:32
(Received via mailing list)
On Mon, May 29, 2006 at 09:08:37PM -0700, Zack Ham 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
685df76ebeb6c0d61ff5e0284830f691?d=identicon&s=25 Jim Cheetham (Guest)
on 2006-05-30 06:32
(Received via mailing list)
On Tue, May 30, 2006 at 05:54:54AM +0200, Bing wrote:
> Jim Cheetham 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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-05-30 06:41
(Received via mailing list)
Zack Ham 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-30 06: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"
>>
5c26728d2db67a78f9097f63ed3c8d37?d=identicon&s=25 Bing (Guest)
on 2006-05-30 07: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.