Yes!! Ruby always has a cool hack!
class Store
def check_inventory
@inventory.to_a.each { ... }
end
end
The reason this works is Cool Ruby. Function number 9.123: as it happens, NilClassimplements a method #to_athat returns []! How amazing is that ?!
edtq ross$ irb --prompt-mode simple
>> nil.to_a
=> []
>> @this_does_not_exist.to_a
=> []
source
share