Archive for February, 2008
Using the [] for conditional processing
The process shown below is and example of using the [] to have the program execute a condition that searches the contents of an array till a match is found. It goes from the top to the bottom to check each and every member of the array to look for a match doing the requested process if it is found and returning nothing if nothing is found.
class BookList
def [](key)
if key.kind_of?(integer)
return @Books[key]
else
for C in 0…@Books.length
return @Books[i] if key == @Book[i].name
end
end
return nil
end
end
Though the above shown method is quite detailed and goes through the array thoroughly, there is a n easier method which we will show with the next post making the whole process not only easier but process faster.
No comments
