Skip to content

Instantly share code, notes, and snippets.

@stevenocchipinti
Created September 12, 2011 03:15
Show Gist options
  • Save stevenocchipinti/1210506 to your computer and use it in GitHub Desktop.
Save stevenocchipinti/1210506 to your computer and use it in GitHub Desktop.
Creating a years <select> element: Is there a better way to do this?
#
# ERB
#
# <% years = (Date.today.year..Date.today.year + 12).collect %>
# <%= f.select(
# 'ccexpy',
# years.zip(years.collect {|y| y.to_s[-2,2]}),
# {},
# { :class => "expirydate" }
# ) %>
#
# EXPECTED VALUE OF years:
#
# [
# [2011, "11"],
# [2012, "12"],
# [2013, "13"],
# [2014, "14"],
# [2015, "15"],
# [2016, "16"],
# [2017, "17"],
# [2018, "18"],
# [2019, "19"],
# [2020, "20"],
# [2021, "21"]
# ]
#
# Pure Ruby example (no ERB)
#
years = (Time.now.year..Time.now.year + 12).collect
p years.zip(years.collect {|y| y.to_s[-2,2]})
@stevenocchipinti
Copy link
Author

That is neater, I'll do that.
So I guess I cant avoid using collect twice then?

@joseph
Copy link

joseph commented Sep 12, 2011

Oh, I didn't inspect the first line carefully. Well, somewhat nicer is:

yr = Time.now.year
(yr..yr+12).collect { |y| [y, y.to_s[2,2]] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment