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 -- Posted via http://www.ruby-forum.com/.
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") -- Phillip Hutchings http://www.sitharus.com/
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
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" > > -jimSweet! Thanks so much jim! -- Posted via http://www.ruby-forum.com/.
On Tuesday, May 30, 2006, at 5:39 AM, 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"? > >Trickier than I first thought. > >TIA, > >Bing > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsa = "/var/www/vhosts/mysite.com/httpdocs/public/file/mypic.jpg" a[/(\w+\.\w+)$/] => ''mypic.jpg'' _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
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
Does File.basename require that the file exists on the local file system? On 5/29/06, Ray Baxter <ray@warmroom.com> wrote:> > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/81557390/attachment.html
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
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
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
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">>On 5/30/06, Ray Baxter <ray@warmroom.com> wrote:> > 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 > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/4edea069/attachment-0001.html
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 -- Posted via http://www.ruby-forum.com/.