4 ms·
I can't help but feel that even the "good" example makes things unnecessarily complicated for the sake of saving one line of code. # original start_dat
by wub 14y ago
I can't help but feel that even the "good" example makes things unnecessarily complicated for the sake of saving one line of code.
# original
start_date, end_date = ["24 Dec 2011", "23 Jan 2013"].map {|d| Date.parse(d) }
# better
start = Date.parse("24 Dec 2011")
end = Date.parse("23 Jan 2013")
- caiusdurling 14y agoQuite probably. I suck at making up "good" examples without taking them from code - couldn't think of anything suitable to borrow from last night. I think my thought process was reading a CSV from stdin with two fields, using String#split instead of the CSV library and ending up with an array with two elements. That would've complicated it even more though. :-)
- Argorak 14y agoThis really depends. If your data already comes in tupels (which often happens in this case), deconstructing assignment can express very well what you are doing. # original start_date, end_date = timeframe.map { |d| Date.parse(d) } # better? start_date = Date.parse(timeframe[0]) end_date = Date.parse(timeframe[1]) For decent Rubyists, reading a map is very easy. On the other hand, both versions are acceptable - I wouldn't bother discussing about either of the options.