I'm writing a Ruby script that accesses a MySQL database using the Mysql2 gem.
After I get a set of results from a query, I want to examine a subset of the rows of the result set (e.g. rows 5 to 8) rather than the whole set.
I know I can do this with a while loop, something like this:
db = Mysql2::Client.new(:host => "myserver", :username => "user", :password => "pass", :database => "books")
rs = db.query "select * from bookslist"
i = 5
while i <= 8
puts rs.entries[i]
i += 1
end
db.close
But I'm aware this is probably not the best way to write this in Ruby. How can I make this more "idiomatic" Ruby code?
(I know my other option is to modify the query to return only the data I want. I still want to know how to do this in Ruby)