I am trying to do some calculation like the following. option
is an empty hash and aes
is an array.
options = {}
aes = [1,2]
test = aes.inject([]) do |array, value|
array << value + 2
array << value -1 if options[:calculation] # here options[:calculation] will be nil
end
I debugged and found that the value of test
is nil
. The output of array << value -1 if options[:calcuation]
is nil
, and I want to return the calculated value in test
. For resolving this issue, I am using the code below, where aes
is an array:
options = {}
aes = [1,2]
test = aes.inject([]) do |array, value|
array << value + 2
array << value -1 if options[:calculation] # here options[:calculation] will be nil
array # I am using `array` here
end
Is this correct, or is there any alternate way to do this?