Ruby Tips #365 – OMFG A Year of Ruby Tips

Today marks the end of a 365 day journey of writing one Ruby programming tip everyday. It has been a long journey to consider new and interesting content in the realm of a Ruby and Rails programmer. I have learned so much and I am very happy to share this content with the world.

For today’s final tip, in the 365 days of Ruby series, let’s look at how the each_slice() method does what it can to arrays. We can iterate over collections and grab the modulus of three and return a result when that happens.

[1,2,3,4,5,6,7,8,9,10].each_with_index do |num, idx|
  puts "#{num}"
  if ((idx + 1) % 3 == 0)
    puts "end of line" 
  end 
end 
 
#=> 1
#=> 2
#=> 3
#=> end of line
#=> 4
#=> 5
#=> 6
#=> end of line
#=> 7
#=> 8
#=> 9
#=> end of line
#=> 10
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Where to go from here? In the coming weeks I plan to edit, revise and compile these Ruby tips in a cohesive work. The updated body of work will be available for purchase in an easy to read format with bonuses on how to utilize these tips further. My hope is programmers of all levels will appreciate, find humor, and enjoy the value these tips will have on your craft. PARTY!

:)