# Within a block passed to "each" can I detect the last time t

**URL:** <https://www.ruby-forum.com/t/within-a-block-passed-to-each-can-i-detect-the-last-time-t/85719>\
**Category:** Ruby\
**Created:** [February 15, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/within-a-block-passed-to-each-can-i-detect-the-last-time-t/85719 "2007-02-15T20:50:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 15, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/within-a-block-passed-to-each-can-i-detect-the-last-time-t/85719/1 "2007-02-15T20:50:04Z")

</div>

Hi,

Does anyone know a way for the below code, to tell when the last block  
is  
called via “each” so that I can detect this and not add a superfluous  
“&” to  
the end of the string being built up?

```
postDataHash.each { |key, value|
  postDataString << "#{key}=#{value}&"
}

```

Tks  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 15, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/within-a-block-passed-to-each-can-i-detect-the-last-time-t/85719/2 "2007-02-15T20:57:06Z")

</div>

On Feb 15, 2007, at 1:49 PM, Greg H. wrote:

> Does anyone know a way for the below code, to tell when the last  
> block is  
> called via “each” so that I can detect this and not add a  
> superfluous “&” to  
> the end of the string being built up?

Doesn’t exactly answer your question, but:

> postDataHash.each { |key, value|  
> postDataString \<\< “#{key}=#{value}&”  
> }

postDataString \<\< postDataHash.map { |k, v| “#{k}=#{v}” }.join("&")

James Edward G. II

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 15, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/within-a-block-passed-to-each-can-i-detect-the-last-time-t/85719/3 "2007-02-15T21:25:09Z")

</div>

excellent - thanks James!
