# While gsub!

**URL:** <https://rubytalk.org/t/while-gsub/39224>\
**Category:** ruby-talk\
**Created:** [19 July 2007 00:14 UTC](https://rubytalk.org/t/while-gsub/39224 "2007-07-19T00:14:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Mcdonald1](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@Ryan\_Mcdonald1](https://rubytalk.org/u/Ryan_Mcdonald1)\
**Post date:** [19 July 2007 00:14 UTC](https://rubytalk.org/t/while-gsub/39224/1 "2007-07-19T00:14:59Z")

</div>

Is there a \*better\* way to perform multiple substitutions to a string  
than this?

string = '/../../....//........////../../etc/passwd'

while string.gsub!(/\.\.\//,'')  
end

it's working as is, but seems odd to me to have an empty loop.. I'm  
just playing with the TCPServer class..

#!/usr/bin/ruby  
require 'socket'

port = 80  
listen = '0.0.0.0'  
header = "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"

httpd = TCPServer.new(listen, port)  
while session = httpd.accept  
&nbsp;&nbsp;request = session.gets  
&nbsp;&nbsp;address = session.addr[3]  
&nbsp;&nbsp;puts "#{address} #{request}"  
&nbsp;&nbsp;askfile = request.scan(/GET (.\*) HTTP/).to\_s  
&nbsp;&nbsp;while askfile.gsub!(/\.\.\//,'')  
&nbsp;&nbsp;end  
&nbsp;&nbsp;reqfile = '/var/www' + askfile  
&nbsp;&nbsp;reqfile += 'index.html' if reqfile == '/var/www/'  
&nbsp;&nbsp;if File.exists?(reqfile)  
&nbsp;&nbsp;&nbsp;&nbsp;file = File.new(reqfile, 'r')  
&nbsp;&nbsp;&nbsp;&nbsp;output = file.readlines  
&nbsp;&nbsp;&nbsp;&nbsp;file.close  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;output = '\<html\>\<head\>\<title\>Not Found\</title\>\</head\>\<body\>'  
&nbsp;&nbsp;&nbsp;&nbsp;output += "\<h2\>Unfortunately, \"#{askfile}\" does not exist on  
this server..\</h2\>"  
&nbsp;&nbsp;&nbsp;&nbsp;output += '\<p\>perhaps you need more fortune:\</p\>'  
&nbsp;&nbsp;&nbsp;&nbsp;output += "\<hr /\>\<p\>#{`/usr/games/fortune`}\</p\>\<hr /\>"  
&nbsp;&nbsp;&nbsp;&nbsp;output += '\</body\>\</html\>'  
&nbsp;&nbsp;end  
&nbsp;&nbsp;session.print header  
&nbsp;&nbsp;session.print output  
&nbsp;&nbsp;session.close  
end

---

<div class="post-metadata">

**Author:** ![Marcel\_Molina\_Jr](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Marcel\_Molina\_Jr](https://rubytalk.org/u/Marcel_Molina_Jr)\
**Post date:** [19 July 2007 00:18 UTC](https://rubytalk.org/t/while-gsub/39224/2 "2007-07-19T00:18:20Z")

</div>

irb(main):001:\>\> string = '/../../....//........////../../etc/passwd'  
=\> "/../../....//........////../../etc/passwd"  
irb(main):002:0\> File.expand\_path(string)  
=\> "/etc/passwd"

marcel

> **···**
>
> On Thu, Jul 19, 2007 at 09:14:59AM +0900, ryemcdonald@gmail.com wrote:
> 
> > Is there a \*better\* way to perform multiple substitutions to a string  
> > than this?
> > 
> > string = '/../../....//........////../../etc/passwd'
> > 
> > while string.gsub!(/\.\.\//,'')  
> > end
> 
> --  
> Marcel Molina Jr. \<marcel@vernix.org\>

---

<div class="post-metadata">

**Author:** ![Ryan\_Mcdonald1](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@Ryan\_Mcdonald1](https://rubytalk.org/u/Ryan_Mcdonald1)\
**Post date:** [19 July 2007 16:40 UTC](https://rubytalk.org/t/while-gsub/39224/3 "2007-07-19T16:40:00Z")

</div>

that's great! thank you

> **···**
>
> On Jul 18, 5:18 pm, "Marcel Molina Jr." \<mar...@vernix.org\> wrote:
> 
> > On Thu, Jul 19, 2007 at 09:14:59AM +0900, ryemcdon...@gmail.com wrote:  
> > \> Is there a \*better\* way to perform multiple substitutions to a string  
> > \> than this?
> > 
> > \> string = '/../../....//........////../../etc/passwd'
> > 
> > \> while string.gsub!(/\.\.\//,'')  
> > \> end
> > 
> > irb(main):001:\>\> string = '/../../....//........////../../etc/passwd'  
> > =\> "/../../....//........////../../etc/passwd"  
> > irb(main):002:0\> File.expand\_path(string)  
> > =\> "/etc/passwd"
> > 
> > marcel  
> > --  
> > Marcel Molina Jr. \<mar...@vernix.org\>
