[ next ] [ prev ] [ contents ] [ up to Appendix B -- Database Example ] Invitation To Ruby

The Ruby Way of Databases

  1: db.prepare(sql) { |statement|
  2:   statement.execute(ARGV[0])
  3:   statement.each { |row|
  4:     puts row[0]
  5:   }
  6: }

Changes

  • The prepare method use a block.
    • A managed statement is passed to the block.
    • When then block is done, the statement will be automatical reclaimed (finished).

  • After executing the statement, it may be used as a iterator.
    • each will walk through each row fetched, one by one.
    • Other Enumerable function are also available on statements (e.g. collect, find).


[ next ] [ prev ] [ contents ] [ up to Appendix B -- Database Example ] Copyright 2002 by Jim Weirich.
All rights reserved.