# I don't get how the method is added in this case

**URL:** <https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693>\
**Category:** Ruby\
**Created:** [September 4, 2007, 1:04am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693 "2007-09-04T01:04:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 4, 2007, 1:04am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693/1 "2007-09-04T01:04:47Z")

</div>

I have the following

#!/usr/bin/ruby -w

require ‘net/http’

h=Net::HTTP.new(‘[www.pragmaticprogrammer.com](http://www.pragmaticprogrammer.com)’,80)  
response=h.get(‘/index.html’,nil)

if(response.message==“OK”)  
puts response.body.scan(/\<img src=“(.\*?)”/m).uniq  
end

I know you can add methods to an existing class, but in the above code,  
there is the following:

puts response.body.scan(/\<img src=“(.\*?)”/m).uniq

I don’t get how the methods are being added to the class in this case.  
Is uniq() being added to scan(), the being added to body() which in turn  
is added to the response object?

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 4, 2007, 1:12am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693/2 "2007-09-04T01:12:28Z")

</div>

Cd Cd wrote:

> ```
> puts response.body.scan(/<img src="(.*?)"/m).uniq
> 
> ```
> 
> end
> 
> I know you can add methods to an existing class, but in the above code,  
> there is the following:
> 
> puts response.body.scan(/\<img src="(.\*?)"/m).uniq
> 
> I don’t get how the methods are being added to the class in this case.  
> Is uniq() being added to scan(), the being added to body() which in turn  
> is added to the response object?  
> They aren’t being added to a class so much as called on the result of  
> each method call. You could rewrite it like this:

b = response.body  
imgs = b.scan(/\<img src="(.\*?)"/m)  
uniq\_imgs = imgs.uniq  
puts uniq\_imgs

HTH

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 4, 2007, 1:47am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693/3 "2007-09-04T01:47:50Z")

</div>

Alex Y. wrote:

> Cd Cd wrote:
> 
> > ```
> > puts response.body.scan(/<img src="(.*?)"/m).uniq
> > 
> > ```
> > 
> > end
> > 
> > I know you can add methods to an existing class, but in the above code,  
> > there is the following:
> > 
> > puts response.body.scan(/\<img src=“(.\*?)”/m).uniq
> > 
> > I don’t get how the methods are being added to the class in this case.  
> > Is uniq() being added to scan(), the being added to body() which in turn  
> > is added to the response object?  
> > They aren’t being added to a class so much as called on the result of  
> > each method call. You could rewrite it like this:
> 
> b = response.body  
> imgs = b.scan(/\<img src=“(.\*?)”/m)  
> uniq\_imgs = imgs.uniq  
> puts uniq\_imgs
> 
> HTH

But if I go something like  
#!/usr/bin/ruby -w

require ‘net/http’

h=Net::HTTP.new(‘[www.pragmaticprogrammer.com](http://www.pragmaticprogrammer.com)’,80)  
response=h.get(‘/index.html’,nil)

if(response.message==“OK”)  
b = response.body  
p b  
end

‘b’ will print out the body of the web page. So ‘b’ in this case would  
be become the receiver (ie imgs = b.scan(/\<img src=“(._?)“/m)), then we  
go from getting the value ‘b’ (via p b) to ‘b’ acting like a method  
(via imgs = b.scan(/\<img src=”(._?)”/m))?

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 4, 2007, 2:07am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693/4 "2007-09-04T02:07:30Z")

</div>

On Tue, Sep 04, 2007, Cd Cd wrote:

> ‘b’ will print out the body of the web page. So ‘b’ in this case would  
> be become the receiver (ie imgs = b.scan(/\<img src="(._?)"/m)), then we  
> go from getting the value ‘b’ (via p b) to ‘b’ acting like a method  
> (via imgs = b.scan(/\<img src="(._?)"/m))?

b is an object in either case. When you ‘p b’, you’re printing out the  
contents of the b object… when you call b.scan, you’re calling scan on  
the b object.

Make more sense?

Ben

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 4, 2007, 2:40am UTC](https://www.ruby-forum.com/t/i-dont-get-how-the-method-is-added-in-this-case/113693/5 "2007-09-04T02:40:14Z")

</div>

Ben B. wrote:

> On Tue, Sep 04, 2007, Cd Cd wrote:
> 
> > ‘b’ will print out the body of the web page. So ‘b’ in this case would  
> > be become the receiver (ie imgs = b.scan(/\<img src="(._?)"/m)), then we  
> > go from getting the value ‘b’ (via p b) to ‘b’ acting like a method  
> > (via imgs = b.scan(/\<img src="(._?)"/m))?
> 
> b is an object in either case. When you ‘p b’, you’re printing out the  
> contents of the b object… when you call b.scan, you’re calling scan on  
> the b object.
> 
> Make more sense?
> 
> Ben

Yes it does.
