Skip to content

Instantly share code, notes, and snippets.

@wezm
Created May 1, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wezm/80da7c457e64da0d64d2 to your computer and use it in GitHub Desktop.
Save wezm/80da7c457e64da0d64d2 to your computer and use it in GitHub Desktop.
Generate listing

Listing

How would you go about generating a page like this in Rails?

screenshot

Each of the letters at the top of the page is a link to #a, #b, etc. Above each row <a name="a"></a> tags need to be added for all the letters that occur between the previous row and this row.

Here is a sample array:

items = ["African", "Ambient", "Asian", "Avant-garde", "Blues", "Breakbeat", "Caribbean", "Comedy", "Country", "Disco", "Downtempo", "Drum & Bass", "Easy listening", "Electro", "Electronic", "Folk", "Hardcore", "Hardstyle", "Hip hop", "House", "Industrial", "Jazz", "Jungle", "Latin", "Pop", "R&B", "Rock", "Soul", "Techno", "Trance"]

See below for a staring point for writing a solution. I have an implementation already, I'm curious how others would solve it.

<a name="a">
["African", "Ambient"]
["Asian", "Avant-garde"]
<a name="b">
["Blues", "Breakbeat"]
<a name="c">
["Caribbean", "Comedy"]
<a name="d">
["Country", "Disco"]
["Downtempo", "Drum & Bass"]
<a name="e">
["Easy listening", "Electro"]
<a name="f">
["Electronic", "Folk"]
<a name="g">
<a name="h">
["Hardcore", "Hardstyle"]
["Hip hop", "House"]
<a name="i">
<a name="j">
["Industrial", "Jazz"]
<a name="k">
<a name="l">
["Jungle", "Latin"]
<a name="m">
<a name="n">
<a name="o">
<a name="p">
<a name="q">
<a name="r">
["Pop", "R&B"]
<a name="s">
["Rock", "Soul"]
<a name="t">
<a name="u">
<a name="v">
<a name="w">
<a name="x">
<a name="y">
<a name="z">
["Techno", "Trance"]
require 'active_support/core_ext/array'
items = ["African", "Ambient", "Asian", "Avant-garde", "Blues", "Breakbeat", "Caribbean", "Comedy", "Country", "Disco", "Downtempo", "Drum & Bass", "Easy listening", "Electro", "Electronic", "Folk", "Hardcore", "Hardstyle", "Hip hop", "House", "Industrial", "Jazz", "Jungle", "Latin", "Pop", "R&B", "Rock", "Soul", "Techno", "Trance"]
items.in_groups_of(2).each do |group|
puts group.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment