[#3006] mismatched quotation — "stevan apter" <apter@...>

ruby documentation uses a punctuation convention i've never seen

13 messages 2000/05/27

[ruby-talk:02934] Re: Iterators...

From: Dave Thomas <Dave@...>
Date: 2000-05-22 23:36:32 UTC
List: ruby-talk #2934
h.fulton@att.net writes:

> In an abitrary iterator, I want to know
> whether I am on the first or last iteration.
> For example:
> 
>   myarray.each { |x| print "{ " if iter_first?
>     print x
>     if iter_last?
>       print " }\n"
>     else
>       print ", "
>     end
>   }

I know it's not a direct answer to your question, but in this
particular case you could write

  print "{ #{myarray.join(', ')} }\n"

If you're writing your own iterators, you could add another parameter
to the block to do this, something like

  myarray.each_with_state { |element, state|

    case state
    when MyIterator::START
      ...
    when MyIterator::MIDDLE
      ...
    when MyIterator::END
      ...
    end
  }

But I agree - it would be an interesting feature to think about.


Dave

In This Thread

Prev Next