Forum: Ruby on Rails Removing part of a String

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.
9f4a65c520a69cd357d830661e172272?d=identicon&s=25 JannaB (Guest)
on 2009-05-07 23:07
(Received via mailing list)
I want to get the names of all files in a given directory. When I
employ the Dir[] method, it returns the name of the files with the
pathname I invoked it with:

      allfiles = Dir["public/images/icons/**"]

So to remedy this, I try to sub out the directory prefixing the
string, as:

      for fil in allfiles do
             render :text => fil.sub( "public/images/icons/",
"" )
      end

Yet even this doesn't remove the directory name. Clearly I am doing
something stooopid, but just don't see it. Can someone please have a
look and see what I am missing here? Thanks you, Janna
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-05-07 23:11
(Received via mailing list)
On May 7, 2009, at 2:06 PM, JannaB wrote:

>      for fil in allfiles do
>             render :text => fil.sub( "public/images/icons/",
> "" )
>      end
>
> Yet even this doesn't remove the directory name. Clearly I am doing
> something stooopid, but just don't see it. Can someone please have a
> look and see what I am missing here? Thanks you, Janna


File.basename("/home/gumby/work/ruby.rb")          #=> "ruby.rb"
File.basename("/home/gumby/work/ruby.rb", ".rb")   #=> "ruby"
Af2ce6689213fdb78913a9662b18da6b?d=identicon&s=25 Rick Lloyd (ricklloyd)
on 2009-05-08 03:10
(Received via mailing list)
Dir.entries("public/images/icons") should do nicely...
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-08 08:00
Rick Lloyd wrote:
> Dir.entries("public/images/icons") should do nicely...

And in any case:

fil = "public/images/icons/hello.ico"
p fil.sub("public/images/icons/", "")

--output:--
"hello.ico"
1042dba6177fdc7b7fa1279215cd6a2e?d=identicon&s=25 Svetlana Vt (svetlana)
on 2009-05-08 08:57
JannaB wrote:
> I want to get the names of all files in a given directory. When I
> employ the Dir[] method, it returns the name of the files with the
> pathname I invoked it with:
>
>       allfiles = Dir["public/images/icons/**"]
>
> So to remedy this, I try to sub out the directory prefixing the
> string, as:
>
>       for fil in allfiles do
>              render :text => fil.sub( "public/images/icons/",
> "" )
>       end
>
> Yet even this doesn't remove the directory name. Clearly I am doing
> something stooopid, but just don't see it. Can someone please have a
> look and see what I am missing here? Thanks you, Janna

require 'pathname'
allfiles = Pathname.new('/opt')
allfiles.children.map{|a| a.basename.to_s}

or

Dir.chdir("/opt") do
allfiles = Dir["**"]
end
This topic is locked and can not be replied to.