Incrementing letters using .next

Dudo picture Dudo · Mar 21, 2013 · Viewed 9.9k times · Source
def home
  letter = 'A'
  @markers = Location.all.to_gmaps4rails do |loc, marker|
    marker.infowindow render_to_string(partial: '/locations/info', 
                                       locals: {object: loc})
    marker.picture({picture: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=#{letter.next!}|9966FF|000000",
                    width: 32,
                    height: 32,
                    shadow_picture: "http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
                    shadow_width: 110,
                    shadow_height: 110,
                    shadow_anchor: [17,36]})
    marker.title "Title - #{loc.name}"
    marker.sidebar render_to_string(partial: '/locations/sidebar', 
                                    locals: {object: loc})
    marker.json({id: loc.id})
  end
end

Cool stuff. So this works. It cycles through the do loop and increments the letter. Problem is, it starts at B. I tried using just letter in the picture, then at the end using letter.next!, and even letter = letter.next, but gmaps throws an error at me.

Is there a way to assign something besides 'A' to letter?

Answer

nicooga picture nicooga · Mar 21, 2013

What about this?

letters = ('A'..'Z').to_a
letters.shift #=> 'A'
letters.shift #=> 'B'

You'll like this one :)

letter = '@'
letter.next! #=> "A"

Check '@ABCD'.codepoints.to_a to see the magic.