cool-address-65779
01/13/2021, 4:37 AMrough-autumn-89795
01/13/2021, 1:57 PMelegant-appointment-49116
01/14/2021, 3:16 AMsilly-train-46493
01/14/2021, 4:11 AMbroad-magazine-64835
01/14/2021, 3:31 PMacoustic-church-66238
01/15/2021, 7:22 AMcold-school-3088
01/15/2021, 6:40 PMwonderful-dusk-20983
01/16/2021, 2:40 PMcuddly-zoo-64600
01/17/2021, 4:33 AMfaint-energy-11966
01/17/2021, 6:12 AMgray-bird-14941
01/17/2021, 9:04 AMfaint-energy-11966
01/17/2021, 2:01 PMwonderful-dusk-20983
01/18/2021, 3:16 AMEnumerable#map when called over a collection class, is there a definitive behaviour on the number of elements that are returned as an output of the of this operation?
I was discussing this with a colleague and below was a conclusion that I came out with. Do let me know your opinions on this.
1. A function always returns the output of last invoked statement. This behaviour is universal to Ruby.
2. As an example if we say [1,2,3,4].map { |number| function(num) }, we can predict number of elements in the output. It would atleast be `nil` in case the function does not return anything.
Worst case if the function does returns `nil` for all the `number`s, the output would contain 4 elements.
My conclusion,
The number of elements in the output is a definitive number, and is equal to the number of elements in the input. The sole reason for this is the implicit returns in Ruby
Explanations
Whatever be the contents of the block passed to the map, it would be invoked only once for each element. As a method invocation returns atleast a nil, hence there would be total number of N invocations where N is the number of elements in the collection.calm-gigabyte-36239
01/18/2021, 4:06 PMbroad-magazine-64835
01/20/2021, 9:46 AMloud-glass-33663
01/20/2021, 2:49 PMdamp-memory-53739
01/21/2021, 3:52 PMaverage-judge-62891
01/22/2021, 4:54 PMaverage-judge-62891
01/22/2021, 4:56 PMcreamy-dawn-73235
01/24/2021, 8:46 AMrefined-vr-28621
01/24/2021, 9:02 AMloud-glass-33663
01/25/2021, 4:48 AMorange-mouse-66795
01/25/2021, 6:09 AMfaint-energy-11966
01/25/2021, 1:54 PMfaint-energy-11966
01/26/2021, 12:52 PMquick-tent-39442
01/26/2021, 6:55 PMplain-keyboard-30874
01/27/2021, 4:33 AMdamp-coat-97256
01/27/2021, 7:35 AMcalm-grass-85557
01/27/2021, 7:35 AMwonderful-dusk-20983
01/27/2021, 10:49 AMso = ServiceClass.new
so.get_some_property
When the value of some_property is nil should the service respond with nil or raise the error saying the value requested is nil and cannot be returned