Forum: Ruby within a block passed to "each" can I detect the last time t

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2007-02-15 20:50
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-02-15 20:57
(Received via mailing list)
On Feb 15, 2007, at 1:49 PM, Greg Hauptmann 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 Gray II
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2007-02-15 21:25
(Received via mailing list)
excellent - thanks James!
This topic is locked and can not be replied to.