Don't miss

I created an open source development tool to quickly & dynamically mock API endpoints. Check it out at GitHub: DuckRails

Heads up

I will no longer maintain arubystory. For new posts, find me at my personal blog. CU there!

Tuesday, May 26, 2015

Ruby & Rails - Cool stuff & tips - S15E01

It's been a while since my last post, I didn't have the time to prepare a tutorial but I'm going to describe some stuff I thought they worth to be mentioned.

Arrays: map & each_with_index

I knew I could iterate through my array's items with an index of their position using the each_with_index method like this:
['a', 'b', 'c'].each_with_index do { |item, index|
  puts "#{index}-#{item}"

What is cool though, is the fact that you can use that index while mapping the elements to new objects. Here's how:
['a', 'b', 'c'] { |item, index|
  { :letter => item,
    :position => index }