||= []
Sometimes whilst writing Ruby code we have to use a pattern that is repeated a lot when building arrays. Idiomatic code to achieve this looks like this:
def build_array
@array = []
(1..array_length).each do
@array << rand(100)
end
@array
end
This can be changed to:
attr_reader :array
def build_array
(1..array_length).each { @array ||=[]; @array << rand(100) }
end