Logan Capaldo
12/7/2006 12:15:00 AM
On Thu, Dec 07, 2006 at 03:12:23AM +0900, Martin DeMello wrote:
> On 12/6/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
> >>
> >> hex_ip.scan(/../).join_with('.') {|i| i.hex}
> > hex_ip.scan(/../).map {|i| i.hex}.join('.')
> >Hey look, the long way is even shorter! Do you just want to conflate
>
> I knew someone would say that :)
>
> >these two because theres room in the interface? That seems like a silly
> >reason. It would also set a bad precendent. (Well hey, delete doesn't
> >take a block, lets add one and pass the deleted item in...)
>
> No, more because map-then-join is a very common pattern, and the
> complementary argument lists let us combine them neatly.
>
> html_table = ary.jmap("\n") {|col|
> col.jmap {|cell| "<td>#{cell}</td>"}
> }
>
> as opposed to
>
> html_table = ary.map {|col|
> col.jmap {|cell| "<td>#{cell}</td>"}.join
> }.join("\n")
>
> The benefit is that the block is the last argument to what is
> conceptually a single operation.
>
I don't see it personally. As far as I am concerned it's two operations.
At least it wasn't just "Hey, theres room for this, why not put it in."
As for the method name, considered just adding a block to join?
array.join("\n") # Normal join
array.join("\n") { |item| "<td>#{item}</td>" }
Of course theres everybodies favorite iterator:
array.inject("") { |s, cell| "#{s}<td>#{cell}</td>\n" }
> martin