# Question about .gsub

**URL:** <https://www.ruby-forum.com/t/question-about-gsub/79602>\
**Category:** Ruby\
**Created:** [December 16, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/question-about-gsub/79602 "2006-12-16T23:53:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 16, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/1 "2006-12-16T23:53:15Z")

</div>

line = `tree /Users/aragon/Documents -d`  
line.each { |l|  
l = l.gsub(/|/,’ ‘)  
l = l.gsub(/-/,’ ‘)  
l = l.gsub(/`/,’ ')

```
               line = l
               puts line
     }

```

I use the gsub commands because the tree command produces the  
following output:

|-- DUMMY  
|-- lesen.rb  
|-- test.txt  
|-- test1.rb  
|-- test2.rb  
`-- tree.rb

Is there a way to reduce the code?

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 12:07am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/2 "2006-12-17T00:07:16Z")

</div>

Jochen K. wrote:

> I use the gsub commands because the tree command produces the  
> following output:
> 
> |-- DUMMY  
> |-- lesen.rb  
> |-- test.txt  
> |-- test1.rb  
> |-- test2.rb  
> `-- tree.rb

Seems a shame to strip off all that tree-like formatting and characters,  
created with so much effort.

> Is there a way to reduce the code?

puts l.gsub!(%r{[`-|]},’ '}

Not tested.

By the way, what are you trying to create? There are better ways to  
produce  
a tree display within Ruby itself.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 12:19am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/3 "2006-12-17T00:19:17Z")

</div>

Paul L. schrieb:

> > ```
> > }
> > 
> > ```
> > 
> > `-- tree.rb
> 
> Seems a shame to strip off all that tree-like formatting and characters,  
> created with so much effort.

I want them to be store in a mysql-database.

> > Is there a way to reduce the code?
> 
> puts l.gsub!(%r{[`-|]},’ '}
> 
> Not tested.
> 
> By the way, what are you trying to create? There are better ways to produce  
> a tree display within Ruby itself.

I want to build a dropdown-menu so the admin ca select the  
homedirectory of an ftpuser.

Could you please point me to a better solution?

Thanx

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 12:35am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/4 "2006-12-17T00:35:19Z")

</div>

Jochen K. wrote:

/ …

> > Seems a shame to strip off all that tree-like formatting and characters,  
> > created with so much effort.
> 
> I want them to be store in a mysql-database.

By “them” do you mean individual file names, or the entire tree  
structure?  
If the former, which parts of the file information do you need?

I ask because Ruby has many terrific ways to extract file information,  
without having to make Windows system calls.

/ …

> > By the way, what are you trying to create? There are better ways to  
> > produce a tree display within Ruby itself.
> 
> I want to build a dropdown-menu so the admin ca select the  
> homedirectory of an ftpuser.
> 
> Could you please point me to a better solution?

Only with better information. How are the FTP users organized? What kind  
of  
drop-down menu, using what GUI library?

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 12:43am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/5 "2006-12-17T00:43:19Z")

</div>

Paul L. schrieb:

> Jochen K. wrote:
> 
> / …
> 
> > > Seems a shame to strip off all that tree-like formatting and characters,  
> > > created with so much effort.  
> > > I want them to be store in a mysql-database.
> 
> By “them” do you mean individual file names, or the entire tree structure?  
> If the former, which parts of the file information do you need?

I need the complete path - no filenames (dirname, basename?).  
I need no additional file information - only the complete path.

> > Could you please point me to a better solution?
> 
> Only with better information. How are the FTP users organized? What kind of  
> drop-down menu, using what GUI library?

All FTP users a stored in a mysql database, uid, gid, MaxMB,  
DownloadBandwith … The admin tool runs under apache and Ruby on  
Rails.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 1:07am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/6 "2006-12-17T01:07:20Z")

</div>

I found a script which solves my problem:

#!/usr/local/bin/ruby

require ‘find’

dirs = ["/Users/aragon/Sites"]  
excludes = []  
for dir in dirs  
Find.find(dir) do |path|  
if FileTest.directory?(path)  
if excludes.include?(File.basename(path))  
Find.prune # Don’t look any further into this directory.  
else  
next  
end  
else  
puts path  
end  
end  
end

Thanx.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 1:25am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/7 "2006-12-17T01:25:22Z")

</div>

Jochen K. wrote:

> Find.find(dir) do |path|  
> end  
> I believe this is more complex than it needs to be, or I didn’t  
> understand  
> your original request. Please see my other post.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 1:21am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/8 "2006-12-17T01:21:21Z")

</div>

Jochen K. wrote:

> > structure? If the former, which parts of the file information do you  
> > need?
> 
> I need the complete path - no filenames (dirname, basename?).  
> I need no additional file information - only the complete path.

Your reply is ambiguous. Do you want the filename to be included in the  
path? “No filenames” implies that the filename should be omitted, but  
“complete path” implies that the filename should included.

> > > Could you please point me to a better solution?
> > 
> > Only with better information. How are the FTP users organized? What kind  
> > of drop-down menu, using what GUI library?
> 
> All FTP users a stored in a mysql database, uid, gid, MaxMB,  
> DownloadBandwith … The admin tool runs under apache and Ruby on  
> Rails.

Is the directory path search related in any way to the FTP list?

I think it would be better to post the Ruby on Rails inquiry in the Ruby  
on  
Rails newsgroup.

Here is a way to get a list of complete file paths for a given pathname  
argument. This example filters for specific graphic file suffixes, but  
you  
can change this to suit your own requirements:

* * *

require ‘find’

files = []

Find.find("/directory/path") { |path|  
files \<\< path if path =~ /.(jpe?g|png|gif)$/i  
}

puts files.sort

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [December 17, 2006, 8:09am UTC](https://www.ruby-forum.com/t/question-about-gsub/79602/9 "2006-12-17T08:09:54Z")

</div>

If the search is simple enough, ‘\*\*’ may be useful:

Dir.glob ‘/tmp/\*\*/\*.jpg’

returns an array of all of the \*.jpg files in or below /tmp:

irb(main):017:0\> puts Pathname.glob ‘/tmp/\*\*/\*.wav’  
(irb):17: warning: parenthesize argument(s) for future version  
/tmp/8ac6ad1933905c064429ffad284508094daccc0d.wav  
/tmp/mocktmp43800.wav  
/tmp/mocktmp77244.wav  
/tmp/testforfileappender\_1.wav  
/tmp/testforfileappender\_2.wav  
/tmp/custaudio/7/snark-2-1.wav  
/tmp/custaudio/7/snark-2-2.wav  
/tmp/custaudio/7/snark-2-3.wav  
=\> nil  
irb(main):018:0\>

or ‘/tmp/\*\*/’ will get you a directory tree.

- James M.
