# Problem with .each do

**URL:** <https://www.ruby-forum.com/t/problem-with-each-do/48847>\
**Category:** Ruby\
**Created:** [November 20, 2005, 11:23am UTC](https://www.ruby-forum.com/t/problem-with-each-do/48847 "2005-11-20T11:23:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@michelle](https://www.ruby-forum.com/u/michelle)\
**Post date:** [November 20, 2005, 11:23am UTC](https://www.ruby-forum.com/t/problem-with-each-do/48847/1 "2005-11-20T11:23:07Z")

</div>

Hiya. The book does things this way, so why doesn’t it work for me?  
What’s going on?

def fromWeb(site, url)  
# get data from website  
h = Net::HTTP.new(site, 80)  
resp, data=h.get(url, nil)

```
# process data
data.each do <- point of second error
	|line|
	if /DGM_navigation/ =~ line do
		# decode page lineage
		step=Array.new
		astep, line = getNextWord(line)
		while astep != nil
			step.push(astep)
			astep, line = getNextWord(line)
		end
		puts "Page Lineage"
		step.each do
			|astep|
			step.each do
				put " "
			end
			puts astep
		end
	end # end DGM_naviation
end # end data.each <- gets "undefined method 'line'", from the second

```

error point above  
end # end def fromWeb

def getNextWord(line)  
i1 = /\>\w+\</ =~ line

```
string_wanted=$&[1..$&.length-1]
string_remaining=$'
return string_wanted, string_remaining

```

end

---

<div class="post-metadata">

**Author:** ![michelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@michelle](https://www.ruby-forum.com/u/michelle)\
**Post date:** [November 20, 2005, 11:47am UTC](https://www.ruby-forum.com/t/problem-with-each-do/48847/2 "2005-11-20T11:47:16Z")

</div>

Lady Michelle B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> |line|
> [... etc. ...]
> 
> ```
> 
> end # end data.each ← gets “undefined method ‘line’”, from the second

It turns out that Net::Http#get no longer returns a pair of values. It  
has changed between ruby version 1.6 (from the edition of the book that  
you probably have) and 1.8. This is explained in the method’s latest  
documentation (from  
[http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html)):

get(path, initheader = nil, dest = nil) {|+body\_segment+| …}

Gets data from path on the connected-to host. header must be a Hash  
like { ‘Accept’ =\> ‘_/_’, … }.

In version 1.1 (ruby 1.6), this method returns a pair of objects, a  
Net::HTTPResponse object and the entity body string. In version 1.2  
(ruby 1.8), this method returns a Net::HTTPResponse object.

If called with a block, yields each fragment of the entity body in  
turn as a string as it is read from the socket. Note that in this  
case, the returned response object will not contain a (meaningful)  
body.

dest argument is obsolete. It still works but you must not use it.

In version 1.1, this method might raise an exception for 3xx  
(redirect). In this case you can get a HTTPResponse object by  
“anException.response”.

In version 1.2, this method never raises exception.

```
# version 1.1 (bundled with Ruby 1.6)
response, body = http.get('/index.html')

# version 1.2 (bundled with Ruby 1.8 or later)
response = http.get('/index.html')

# using block
File.open('result.txt', 'w') {|f|
  http.get('/~foo/') do |str|
    f.write str
  end
}

```

See also Net::HTTPResponse within that same documentation.

---

<div class="post-metadata">

**Author:** ![michelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@michelle](https://www.ruby-forum.com/u/michelle)\
**Post date:** [November 20, 2005, 11:51am UTC](https://www.ruby-forum.com/t/problem-with-each-do/48847/3 "2005-11-20T11:51:18Z")

</div>

Lady Michelle B. wrote:

> ```
> |line|
> 
> ```

Try `data.each do |line|’. It may be the line break that’s causing  
trouble.

> ```
> |astep|
> 
> ```

Same here.

> def getNextWord(line)  
> i1 = /\>\w+\</ =~ line
> 
> ```
> string_wanted=$&[1..$&.length-1]
> string_remaining=$'
> return string_wanted, string_remaining
> 
> ```
> 
> end

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![michelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@michelle](https://www.ruby-forum.com/u/michelle)\
**Post date:** [November 20, 2005, 12:43pm UTC](https://www.ruby-forum.com/t/problem-with-each-do/48847/4 "2005-11-20T12:43:34Z")

</div>

Sorry, it actually seems to work even with the line break. My bad.

Cheers,  
Daniel
