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

C

Cd Cd

I have the following

#!/usr/bin/ruby -w

require 'net/http'

h=Net::HTTP.new('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?
 
A

Alex Young

Cd said:
I have the following

#!/usr/bin/ruby -w

require 'net/http'

h=Net::HTTP.new('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?
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
 
C

Cd Cd

Alex said:
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',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))?
 
B

Ben Bleything

'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
 
C

Cd Cd

Ben said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top