Esempio n. 1
0
File: gdbm.c Progetto: knugie/ruby
/*
 * call-seq:
 *      GDBM.open(filename, mode = 0666, flags = nil)
 *      GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... }
 *
 * If called without a block, this is synonymous to GDBM::new.
 * If a block is given, the new GDBM instance will be passed to the block
 * as a parameter, and the corresponding database file will be closed
 * after the execution of the block code has been finished.
 *
 * Example for an open call with a block:
 *
 *   require 'gdbm'
 *   GDBM.open("fruitstore.db") do |gdbm|
 *     gdbm.each_pair do |key, value|
 *       print "#{key}: #{value}\n"
 *     end
 *   end
 */
static VALUE
fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = fgdbm_s_alloc(klass);

    if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
        return Qnil;
    }

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, obj, fgdbm_close, obj);
    }

    return obj;
}
Esempio n. 2
0
/*
 * call-seq:
 *      GDBM.open(filename, mode = 0666, flags = nil)
 *      GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... }
 *
 * If called without a block, this is synonymous to GDBM::new.
 * If a block is given, the new GDBM instance will be passed to the block
 * as a parameter, and the corresponding database file will be closed
 * after the execution of the block code has been finished.
 *
 * Example for an open call with a block:
 *
 *   require 'gdbm'
 *   GDBM.open("fruitstore.db") do |gdbm|
 *     gdbm.each_pair do |key, value|
 *       print "#{key}: #{value}\n"
 *     end
 *   end
 */
static VALUE
fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);

    if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
        return Qnil;
    }

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, obj, fgdbm_close, obj);
    }

    return obj;
}