Syntax sugar idea for loops

P

pmak

I was working with a proprietary programming language called Traction
<www.tractionsoftware.com>, and I noticed something pretty cool they
had in their loop constructs.

Consider the following Ruby code:

every_other = true
people.each do |person|
every_other = !every_other
row_class = every_other ? 'row1' : 'row2'
puts "<tr class=#{row_class}><td>#{person.name}</td>
<td>#{person.email}</td></tr>"
end

This code prints out an HTML table of peoples' names and e-mail
addresses. The <tr> tags alternate with having class="row1" and
class="row2", allowing every other row to be colored differently.

Consider these two lines of the code, though:

every_other = true
every_other = !every_other

These lines could be replaced by syntax sugar. Traction has the
following "Special Loop Tags":

<loop.first>: true if this is the first iteration of the loop
<loop.inner> true if this is not the first or last iteration of the
loop
<loop.odd> true if this is an odd iteration
<loop.last>: true if this is the last iteration of the loop

(In the case of nested loops, they apply to the innermost loop.) Maybe
Ruby could use this sort of syntax sugar, too? So in my example above,
there would be a built-in variable that replaces my "every_other"
variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top