# Removing part of a String

**URL:** <https://www.ruby-forum.com/t/removing-part-of-a-string/167461>\
**Category:** Rails\
**Created:** [May 7, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461 "2009-05-07T23:07:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 7, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461/1 "2009-05-07T23:07:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 7, 2009, 11:11pm UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461/2 "2009-05-07T23:11:25Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 8, 2009, 3:10am UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461/3 "2009-05-08T03:10:07Z")

</div>

Dir.entries(“public/images/icons”) should do nicely…

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 8, 2009, 8:57am UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461/4 "2009-05-08T08:57:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 8, 2009, 8:00am UTC](https://www.ruby-forum.com/t/removing-part-of-a-string/167461/5 "2009-05-08T08:00:50Z")

</div>

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”
