# How to use gsub method?

**URL:** <https://www.ruby-forum.com/t/how-to-use-gsub-method/147282>\
**Category:** Ruby\
**Created:** [August 30, 2008, 8:04am UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282 "2008-08-30T08:04:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 30, 2008, 8:04am UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282/1 "2008-08-30T08:04:17Z")

</div>

hi

sorry!  
i can not speak english well…

This is how I know…  
“abcdef”.gsub(/c/) {|h| puts h}

but, Why do I get an error that my code…

this is my code

require ‘net/http’

page= Net::HTTP.new(‘www.google.co.kr’,80)

reg = page.get(’/index.html’)

reg.gsub(/a/) {|gg| puts gg}  
puts reg

thank you…

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 30, 2008, 8:22am UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282/2 "2008-08-30T08:22:43Z")

</div>

On Sat, Aug 30, 2008 at 2:59 PM, Hwang In-seok [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> reg.gsub(/a/) {|gg| puts gg}  
> puts reg
> 
> thank you…

String#gsub does string substitution, but it cannot replace /a/ with  
nil, and the return value of ‘puts’ is nil.  
Maybe you want to try String#scan instead, like this:

```
p reg.scan(/a/)

```

^ manveru

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 30, 2008, 8:43am UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282/3 "2008-08-30T08:43:26Z")

</div>

Michael F. wrote:

> ```
> p reg.scan(/a/)
> 
> ```
> 
> ^ manveru

thank you…  
but, ‘gsub’ and ‘scan’ both have the same error.

error is…  
http.rb:9: private method `scan’ called for #\<Net::HTTPOK 200 OK  
readbody=true\> (NoMethodError)

private method is what mean??  
i don’t understand…

please teach me.

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 30, 2008, 9:08am UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282/4 "2008-08-30T09:08:49Z")

</div>

This is your code :  
require ‘net/http’

page= Net::HTTP.new(‘www.google.co.kr’,80)

reg = page.get(’/index.html’)

reg.gsub(/a/) {|gg| puts gg}  
puts reg

Your are trying to use gsub on the object returned by Net::HTTP , which  
isn’t a string . Your code should look like this :

require ‘net/http’

page= Net::HTTP.new(‘www.google.co.kr’,80)

reg = page.get(’/index.html’)

reg.body.gsub!(/a/) {|gg| puts gg}  
puts reg.body

Pay attention to the gsub! , which modifies the string in-place .Judging  
by your script,that is what you want .

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 30, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/how-to-use-gsub-method/147282/5 "2008-08-30T13:55:07Z")

</div>

wow, thank you!! vhaerun

Thanks to the learned

Icy good weekend.
