How can I turn an array of elements into uppercase? Expected output would be:
[\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\"]
=> [\"M
This should work:
Day.weekday.map(&:name).map(&:upcase)
Or, if you want to save some CPU cycles
Day.weekday.map{|wd| wd.name.upcase}
If you want to return an uppercased array, use #map:
array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
# Return the uppercased version.
array.map(&:upcase)
=> ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"]
# Return the original, unmodified array.
array
=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
As you can see, the original array is not modified, but you can use the uppercased return value from #map anywhere you can use an expression.
If you want to uppercase the array in-place, use #map! instead:
array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
array.map!(&:upcase)
array
=> ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"]
In your example, replace 'each' with 'map'.
While 'each' iterates through your array, it doesn't create a new array containing the values returned by the block.