Blog - .NET, ASP.NET, AJAX, Ruby and more
  • Google Banner 2

Ruby Beauty – Iteration with an Index

  1. I just love how expressive and clean Ruby’s syntax is.  How often do you need to iterate over a collection and get a count as well?  That sort of code is ugly in something like C#, but in Ruby:
people = %w{Dave Bill Mike Mark John} # Simple collection of names
people.each_with_index { |name, index| puts "#{index}: #{name}" }

And that displays:

0: Dave
1: Bill
2: Mike
3: Mark
4: John

each_with_index – simple and elegant.

And what’s the C# version of something like this look like? Blah :)

This entry was posted in Ruby and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

Damien White is a developer in Connecticut that simply loves coding. Ever since he was young, he has had a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. He’s happy to share what he knows in his quest to learn as much as possible.