Reference Information. I need to determine the number of records in a remote location. Entries are sequentially numbered (starting with 0, without spaces) and can only be obtained one after the other based on their number.
The method for retrieving records over the network returns the true value on success and the false value on error. Here is an example:
fetch_record(0)
fetch_record(1)
fetch_record(2)
fetch_record(3)
I am trying to find an elegant way to calculate how many times I can call fetch_recordwith an increase in the argument until it returns nil(3 times in the example above).
What have i tried so far
Given this simplified implementation for testing purposes:
def fetch_record(index)
raise if index > 3
true if index < 3
end
Here are my attempts:
A while , , , :
i = 0
i += 1 while fetch_record(i)
i
step break , :
0.step(by: 1).each { |i| break i unless fetch_record(i) }
:
0.step(by: 1).lazy.map { |i| fetch_record(i) }.take_while(&:itself).count
?