Created
September 12, 2011 03:15
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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]}) |
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
That is neater, I'll do that.
So I guess I cant avoid using collect twice then?