File: C:/Ruby27-x64/share/doc/ruby/html/GDBM.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class GDBM - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div class="nav-section">
<h3>Table of Contents</h3>
<ul class="link-list" role="directory">
<li><a href="#class-GDBM-label-Summary">Summary</a>
<li><a href="#class-GDBM-label-Description">Description</a>
<li><a href="#class-GDBM-label-Examples">Examples</a>
<li><a href="#class-GDBM-label-Links">Links</a>
</ul>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Object.html">Object</a>
</div>
<div id="includes-section" class="nav-section">
<h3>Included Modules</h3>
<ul class="link-list">
<li><a class="include" href="Enumerable.html">Enumerable</a>
</ul>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-c-open">::open</a>
<li ><a href="#method-i-5B-5D">#[]</a>
<li ><a href="#method-i-5B-5D-3D">#[]=</a>
<li ><a href="#method-i-cachesize-3D">#cachesize=</a>
<li ><a href="#method-i-clear">#clear</a>
<li ><a href="#method-i-close">#close</a>
<li ><a href="#method-i-closed-3F">#closed?</a>
<li ><a href="#method-i-delete">#delete</a>
<li ><a href="#method-i-delete_if">#delete_if</a>
<li ><a href="#method-i-each">#each</a>
<li ><a href="#method-i-each_key">#each_key</a>
<li ><a href="#method-i-each_pair">#each_pair</a>
<li ><a href="#method-i-each_value">#each_value</a>
<li ><a href="#method-i-empty-3F">#empty?</a>
<li ><a href="#method-i-fastmode-3D">#fastmode=</a>
<li ><a href="#method-i-fetch">#fetch</a>
<li ><a href="#method-i-has_key-3F">#has_key?</a>
<li ><a href="#method-i-has_value-3F">#has_value?</a>
<li ><a href="#method-i-include-3F">#include?</a>
<li ><a href="#method-i-invert">#invert</a>
<li ><a href="#method-i-key">#key</a>
<li ><a href="#method-i-key-3F">#key?</a>
<li ><a href="#method-i-keys">#keys</a>
<li ><a href="#method-i-length">#length</a>
<li ><a href="#method-i-member-3F">#member?</a>
<li ><a href="#method-i-reject">#reject</a>
<li ><a href="#method-i-reject-21">#reject!</a>
<li ><a href="#method-i-reorganize">#reorganize</a>
<li ><a href="#method-i-replace">#replace</a>
<li ><a href="#method-i-select">#select</a>
<li ><a href="#method-i-shift">#shift</a>
<li ><a href="#method-i-size">#size</a>
<li ><a href="#method-i-store">#store</a>
<li ><a href="#method-i-sync">#sync</a>
<li ><a href="#method-i-syncmode-3D">#syncmode=</a>
<li ><a href="#method-i-to_a">#to_a</a>
<li ><a href="#method-i-to_hash">#to_hash</a>
<li ><a href="#method-i-update">#update</a>
<li ><a href="#method-i-value-3F">#value?</a>
<li ><a href="#method-i-values">#values</a>
<li ><a href="#method-i-values_at">#values_at</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-GDBM">
<h1 id="class-GDBM" class="class">
class GDBM
</h1>
<section class="description">
<h2 id="class-GDBM-label-Summary">Summary<span><a href="#class-GDBM-label-Summary">¶</a> <a href="#top">↑</a></span></h2>
<p>Ruby extension for GNU dbm (gdbm) – a simple database engine for storing key-value pairs on disk.</p>
<h2 id="class-GDBM-label-Description">Description<span><a href="#class-GDBM-label-Description">¶</a> <a href="#top">↑</a></span></h2>
<p>GNU dbm is a library for simple databases. A database is a file that stores key-value pairs. Gdbm allows the user to store, retrieve, and delete data by key. It furthermore allows a non-sorted traversal of all key-value pairs. A gdbm database thus provides the same functionality as a hash. As with objects of the <a href="Hash.html"><code>Hash</code></a> class, elements can be accessed with <code>[]</code>. Furthermore, <a href="GDBM.html"><code>GDBM</code></a> mixes in the <a href="Enumerable.html"><code>Enumerable</code></a> module, thus providing convenient methods such as <a href="Enumerable.html#method-i-find"><code>find</code></a>, <a href="Enumerable.html#method-i-collect"><code>collect</code></a>, <a href="Enumerable.html#method-i-map"><code>map</code></a>, etc.</p>
<p>A process is allowed to open several different databases at the same time. A process can open a database as a “reader” or a “writer”. Whereas a reader has only read-access to the database, a writer has read- and write-access. A database can be accessed either by any number of readers or by exactly one writer at the same time.</p>
<h2 id="class-GDBM-label-Examples">Examples<span><a href="#class-GDBM-label-Examples">¶</a> <a href="#top">↑</a></span></h2>
<ol><li>
<p>Opening/creating a database, and filling it with some entries:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'gdbm'</span>
<span class="ruby-identifier">gdbm</span> = <span class="ruby-constant">GDBM</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"fruitstore.db"</span>)
<span class="ruby-identifier">gdbm</span>[<span class="ruby-string">"ananas"</span>] = <span class="ruby-string">"3"</span>
<span class="ruby-identifier">gdbm</span>[<span class="ruby-string">"banana"</span>] = <span class="ruby-string">"8"</span>
<span class="ruby-identifier">gdbm</span>[<span class="ruby-string">"cranberry"</span>] = <span class="ruby-string">"4909"</span>
<span class="ruby-identifier">gdbm</span>.<span class="ruby-identifier">close</span>
</pre>
</li><li>
<p>Reading out a database:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'gdbm'</span>
<span class="ruby-identifier">gdbm</span> = <span class="ruby-constant">GDBM</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">"fruitstore.db"</span>)
<span class="ruby-identifier">gdbm</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">print</span> <span class="ruby-node">"#{key}: #{value}\n"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">gdbm</span>.<span class="ruby-identifier">close</span>
</pre>
<p>produces</p>
<pre>banana: 8
ananas: 3
cranberry: 4909</pre>
</li></ol>
<h2 id="class-GDBM-label-Links">Links<span><a href="#class-GDBM-label-Links">¶</a> <a href="#top">↑</a></span></h2>
<ul><li>
<p><a href="http://www.gnu.org/software/gdbm">www.gnu.org/software/gdbm</a>/</p>
</li></ul>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="FAST">FAST
<dd><p>flag for new and <a href="Kernel.html#method-i-open"><code>open</code></a>. this flag is obsolete for gdbm >= 1.8</p>
<dt id="NEWDB">NEWDB
<dd><p>open database as a writer; overwrite any existing databases </p>
<dt id="NOLOCK">NOLOCK
<dd><p>flag for new and <a href="Kernel.html#method-i-open"><code>open</code></a></p>
<dt id="READER">READER
<dd><p>open database as a reader</p>
<dt id="SYNC">SYNC
<dd><p>flag for new and <a href="Kernel.html#method-i-open"><code>open</code></a>. only for gdbm >= 1.8</p>
<dt id="VERSION">VERSION
<dd><p>version of the gdbm library</p>
<dt id="WRCREAT">WRCREAT
<dd><p>open database as a writer; if the database does not exist, create a new one</p>
<dt id="WRITER">WRITER
<dd><p>open database as a writer</p>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(filename, mode = 0666, flags = nil)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a new <a href="GDBM.html"><code>GDBM</code></a> instance by opening a gdbm file named <em>filename</em>. If the file does not exist, a new file with file mode <em>mode</em> will be created. <em>flags</em> may be one of the following:</p>
<ul><li>
<p><strong>READER</strong> - open as a reader</p>
</li><li>
<p><strong>WRITER</strong> - open as a writer</p>
</li><li>
<p><strong>WRCREAT</strong> - open as a writer; if the database does not exist, create a new one</p>
</li><li>
<p><strong>NEWDB</strong> - open as a writer; overwrite any existing databases</p>
</li></ul>
<p>The values <strong>WRITER</strong>, <strong>WRCREAT</strong> and <strong>NEWDB</strong> may be combined with the following values by bitwise or:</p>
<ul><li>
<p><strong>SYNC</strong> - cause all database operations to be synchronized to the disk</p>
</li><li>
<p><strong>NOLOCK</strong> - do not lock the database file</p>
</li></ul>
<p>If no <em>flags</em> are specified, the <a href="GDBM.html"><code>GDBM</code></a> object will try to open the database file as a writer and will create it if it does not already exist (cf. flag <code>WRCREAT</code>). If this fails (for instance, if another process has already opened the database as a reader), it will try to open the database file as a reader (cf. flag <code>READER</code>).</p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE file, vmode, vflags;
GDBM_FILE dbm;
struct dbmdata *dbmp;
int mode, flags = 0;
TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
}
else if (NIL_P(vmode)) {
mode = -1; /* return nil if DB does not exist */
}
else {
mode = NUM2INT(vmode);
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
FilePathValue(file);
#ifdef GDBM_CLOEXEC
/* GDBM_CLOEXEC is available since gdbm 1.10. */
flags |= GDBM_CLOEXEC;
#endif
if (flags & RUBY_GDBM_RW_BIT) {
flags &= ~RUBY_GDBM_RW_BIT;
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
flags, mode, MY_FATAL_FUNC);
}
else {
dbm = 0;
if (mode >= 0)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
if (!dbm)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
if (!dbm)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_READER|flags, 0, MY_FATAL_FUNC);
}
if (dbm) {
rb_fd_fix_cloexec(gdbm_fdesc(dbm));
}
if (!dbm) {
if (mode == -1) return Qnil;
if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
gdbm_errno == GDBM_CANT_BE_READER ||
gdbm_errno == GDBM_CANT_BE_WRITER)
rb_sys_fail_str(file);
else
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
if (dbmp->di_dbm)
gdbm_close(dbmp->di_dbm);
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-c-open" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
open(filename, mode = 0666, flags = nil)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
open(filename, mode = 0666, flags = nil) { |gdbm| ... }
</span>
</div>
<div class="method-description">
<p>If called without a block, this is synonymous to <a href="GDBM.html#method-c-new"><code>GDBM::new</code></a>. If a block is given, the new <a href="GDBM.html"><code>GDBM</code></a> 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.</p>
<p>Example for an open call with a block:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'gdbm'</span>
<span class="ruby-constant">GDBM</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">"fruitstore.db"</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">gdbm</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">gdbm</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">print</span> <span class="ruby-node">"#{key}: #{value}\n"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>
<div class="method-source-code" id="open-source">
<pre>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;
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-5B-5D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gdbm[key] → value
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Retrieves the <em>value</em> corresponding to <em>key</em>.</p>
<div class="method-source-code" id="5B-5D-source">
<pre>static VALUE
fgdbm_aref(VALUE obj, VALUE keystr)
{
return rb_gdbm_fetch3(obj, keystr);
}</pre>
</div>
</div>
</div>
<div id="method-i-5B-5D-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gdbm[key]= value → value
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Associates the value <em>value</em> with the specified <em>key</em>.</p>
<div class="method-source-code" id="5B-5D-3D-source">
<pre>static VALUE
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
GDBM_FILE dbm;
rb_gdbm_modify(obj);
ExportStringValue(keystr);
ExportStringValue(valstr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
val.dptr = RSTRING_PTR(valstr);
val.dsize = RSTRING_LENINT(valstr);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
if (errno == EPERM) rb_sys_fail(0);
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
return valstr;
}</pre>
</div>
</div>
</div>
<div id="method-i-cachesize-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
cachesize = size → size
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the size of the internal bucket cache to <em>size</em>.</p>
<div class="method-source-code" id="cachesize-3D-source">
<pre>static VALUE
fgdbm_set_cachesize(VALUE obj, VALUE val)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
int optval;
GetDBM2(obj, dbmp, dbm);
optval = FIX2INT(val);
if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
return val;
}</pre>
</div>
</div>
</div>
<div id="method-i-clear" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
clear → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Removes all the key-value pairs within <em>gdbm</em>.</p>
<div class="method-source-code" id="clear-source">
<pre>static VALUE
fgdbm_clear(VALUE obj)
{
datum key, nextkey;
struct dbmdata *dbmp;
GDBM_FILE dbm;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
#if 0
while (key = gdbm_firstkey(dbm), key.dptr) {
if (gdbm_delete(dbm, key)) {
free(key.dptr);
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
free(key.dptr);
}
#else
while (key = gdbm_firstkey(dbm), key.dptr) {
for (; key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
if (gdbm_delete(dbm, key)) {
free(key.dptr);
if (nextkey.dptr) free(nextkey.dptr);
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
free(key.dptr);
}
}
#endif
dbmp->di_size = 0;
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-close" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
close → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Closes the associated database file.</p>
<div class="method-source-code" id="close-source">
<pre>static VALUE
fgdbm_close(VALUE obj)
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
gdbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-closed-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
closed? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the associated database file has been closed.</p>
<div class="method-source-code" id="closed-3F-source">
<pre>static VALUE
fgdbm_closed(VALUE obj)
{
struct dbmdata *dbmp;
TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
if (dbmp->di_dbm == 0)
return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-delete" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
delete(key) → value or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Removes the key-value-pair with the specified <em>key</em> from this database and returns the corresponding <em>value</em>. Returns nil if the database is empty.</p>
<div class="method-source-code" id="delete-source">
<pre>static VALUE
fgdbm_delete(VALUE obj, VALUE keystr)
{
VALUE valstr;
valstr = fgdbm_fetch(obj, keystr, Qnil);
rb_gdbm_delete(obj, keystr);
return valstr;
}</pre>
</div>
</div>
</div>
<div id="method-i-delete_if" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
delete_if { |key, value| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Deletes every key-value pair from <em>gdbm</em> for which <em>block</em> evaluates to true.</p>
<div class="method-source-code" id="delete_if-source">
<pre>static VALUE
fgdbm_delete_if(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr;
VALUE ret, ary = rb_ary_tmp_new(0);
long i;
int status = 0, n;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
OBJ_FREEZE(keystr);
valstr = rb_gdbm_fetch2(dbm, keystr);
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
if (status != 0) break;
if (RTEST(ret)) rb_ary_push(ary, keystr);
GetDBM2(obj, dbmp, dbm);
}
for (i = 0; i < RARRAY_LEN(ary); i++)
rb_gdbm_delete(obj, RARRAY_AREF(ary, i));
if (status) rb_jump_tag(status);
if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
rb_ary_clear(ary);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-each" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_pair { |key, value| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Executes <em>block</em> for each key in the database, passing the <em>key</em> and the corresponding <em>value</em> as a parameter.</p>
<div class="method-source-code" id="each-source">
<pre>static VALUE
fgdbm_each_pair(VALUE obj)
{
GDBM_FILE dbm;
struct dbmdata *dbmp;
VALUE keystr;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_key" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_key { |key| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Executes <em>block</em> for each key in the database, passing the <em>key</em> as a parameter.</p>
<div class="method-source-code" id="each_key-source">
<pre>static VALUE
fgdbm_each_key(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(keystr);
GetDBM2(obj, dbmp, dbm);
}
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_pair" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_pair { |key, value| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Executes <em>block</em> for each key in the database, passing the <em>key</em> and the corresponding <em>value</em> as a parameter.</p>
<div class="method-source-code" id="each_pair-source">
<pre>static VALUE
fgdbm_each_pair(VALUE obj)
{
GDBM_FILE dbm;
struct dbmdata *dbmp;
VALUE keystr;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_value" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_value { |value| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Executes <em>block</em> for each key in the database, passing the corresponding <em>value</em> as a parameter.</p>
<div class="method-source-code" id="each_value-source">
<pre>static VALUE
fgdbm_each_value(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_gdbm_fetch2(dbm, keystr));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-empty-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
empty? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the database is empty.</p>
<div class="method-source-code" id="empty-3F-source">
<pre>static VALUE
fgdbm_empty_p(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
GDBM_FILE dbm;
GetDBM(obj, dbmp);
if (dbmp->di_size < 0) {
dbm = dbmp->di_dbm;
key = gdbm_firstkey(dbm);
if (key.dptr) {
free(key.dptr);
return Qfalse;
}
return Qtrue;
}
if (dbmp->di_size == 0) return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-fastmode-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fastmode = boolean → boolean
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Turns the database's fast mode on or off. If fast mode is turned on, gdbm does not wait for writes to be flushed to the disk before continuing.</p>
<p>This option is obsolete for gdbm >= 1.8 since fast mode is turned on by default. See also: <a href="GDBM.html#method-i-syncmode-3D"><code>syncmode=</code></a></p>
<div class="method-source-code" id="fastmode-3D-source">
<pre>static VALUE
fgdbm_set_fastmode(VALUE obj, VALUE val)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
int optval;
GetDBM2(obj, dbmp, dbm);
optval = 0;
if (RTEST(val))
optval = 1;
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
return val;
}</pre>
</div>
</div>
</div>
<div id="method-i-fetch" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fetch(key [, default]) → value
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Retrieves the <em>value</em> corresponding to <em>key</em>. If there is no value associated with <em>key</em>, <em>default</em> will be returned instead.</p>
<div class="method-source-code" id="fetch-source">
<pre>static VALUE
fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
{
VALUE keystr, valstr, ifnone;
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
valstr = fgdbm_fetch(obj, keystr, ifnone);
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
rb_raise(rb_eIndexError, "key not found");
return valstr;
}</pre>
</div>
</div>
</div>
<div id="method-i-has_key-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
has_key?(k) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given key <em>k</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="has_key-3F-source">
<pre>static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
datum key;
struct dbmdata *dbmp;
GDBM_FILE dbm;
long len;
ExportStringValue(keystr);
len = RSTRING_LENINT(keystr);
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr);
key.dsize = (int)len;
GetDBM2(obj, dbmp, dbm);
if (gdbm_exists(dbm, key))
return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-has_value-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
has_value?(v) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given value <em>v</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="has_value-3F-source">
<pre>static VALUE
fgdbm_has_value(VALUE obj, VALUE valstr)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr2;
ExportStringValue(valstr);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr2 = rb_gdbm_fetch2(dbm, keystr);
if (!NIL_P(valstr2) &&
(int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
(int)RSTRING_LEN(valstr)) == 0) {
return Qtrue;
}
}
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-include-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
include?(k) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given key <em>k</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="include-3F-source">
<pre>static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
datum key;
struct dbmdata *dbmp;
GDBM_FILE dbm;
long len;
ExportStringValue(keystr);
len = RSTRING_LENINT(keystr);
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr);
key.dsize = (int)len;
GetDBM2(obj, dbmp, dbm);
if (gdbm_exists(dbm, key))
return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-invert" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
invert → hash
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a hash created by using <em>gdbm</em>'s values as keys, and the keys as values.</p>
<div class="method-source-code" id="invert-source">
<pre>static VALUE
fgdbm_invert(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr;
VALUE hash = rb_hash_new();
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr = rb_gdbm_fetch2(dbm, keystr);
rb_hash_aset(hash, valstr, keystr);
}
return hash;
}</pre>
</div>
</div>
</div>
<div id="method-i-key" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
key(value) → key
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the <em>key</em> for a given <em>value</em>. If several keys may map to the same value, the key that is found first will be returned.</p>
<div class="method-source-code" id="key-source">
<pre>static VALUE
fgdbm_key(VALUE obj, VALUE valstr)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr2;
ExportStringValue(valstr);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr2 = rb_gdbm_fetch2(dbm, keystr);
if (!NIL_P(valstr2) &&
(int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
(int)RSTRING_LEN(valstr)) == 0) {
return keystr;
}
}
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-key-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
key?(k) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given key <em>k</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="key-3F-source">
<pre>static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
datum key;
struct dbmdata *dbmp;
GDBM_FILE dbm;
long len;
ExportStringValue(keystr);
len = RSTRING_LENINT(keystr);
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr);
key.dsize = (int)len;
GetDBM2(obj, dbmp, dbm);
if (gdbm_exists(dbm, key))
return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-keys" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
keys → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of all keys of this database.</p>
<div class="method-source-code" id="keys-source">
<pre>static VALUE
fgdbm_keys(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_ary_push(ary, keystr);
}
return ary;
}</pre>
</div>
</div>
</div>
<div id="method-i-length" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
length → fixnum
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the number of key-value pairs in this database.</p>
<div class="method-source-code" id="length-source">
<pre>static VALUE
fgdbm_length(VALUE obj)
{
datum key, nextkey;
struct dbmdata *dbmp;
GDBM_FILE dbm;
int i = 0;
GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
free(key.dptr);
i++;
}
dbmp->di_size = i;
return INT2FIX(i);
}</pre>
</div>
</div>
</div>
<div id="method-i-member-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
member?(k) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given key <em>k</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="member-3F-source">
<pre>static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
datum key;
struct dbmdata *dbmp;
GDBM_FILE dbm;
long len;
ExportStringValue(keystr);
len = RSTRING_LENINT(keystr);
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr);
key.dsize = (int)len;
GetDBM2(obj, dbmp, dbm);
if (gdbm_exists(dbm, key))
return Qtrue;
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-reject" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
reject { |key, value| block } → hash
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a hash copy of <em>gdbm</em> where all key-value pairs from <em>gdbm</em> for which <em>block</em> evaluates to true are removed. See also: <a href="GDBM.html#method-i-delete_if"><code>delete_if</code></a></p>
<div class="method-source-code" id="reject-source">
<pre>static VALUE
fgdbm_reject(VALUE obj)
{
return rb_hash_delete_if(fgdbm_to_hash(obj));
}</pre>
</div>
</div>
</div>
<div id="method-i-reject-21" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
reject! { |key, value| block } → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Deletes every key-value pair from <em>gdbm</em> for which <em>block</em> evaluates to true.</p>
<div class="method-source-code" id="reject-21-source">
<pre>static VALUE
fgdbm_delete_if(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr;
VALUE ret, ary = rb_ary_tmp_new(0);
long i;
int status = 0, n;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
OBJ_FREEZE(keystr);
valstr = rb_gdbm_fetch2(dbm, keystr);
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
if (status != 0) break;
if (RTEST(ret)) rb_ary_push(ary, keystr);
GetDBM2(obj, dbmp, dbm);
}
for (i = 0; i < RARRAY_LEN(ary); i++)
rb_gdbm_delete(obj, RARRAY_AREF(ary, i));
if (status) rb_jump_tag(status);
if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
rb_ary_clear(ary);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-reorganize" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
reorganize → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Reorganizes the database file. This operation removes reserved space of elements that have already been deleted. It is only useful after a lot of deletions in the database.</p>
<div class="method-source-code" id="reorganize-source">
<pre>static VALUE
fgdbm_reorganize(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
gdbm_reorganize(dbm);
rb_fd_fix_cloexec(gdbm_fdesc(dbm));
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-replace" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
replace(other) → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Replaces the content of <em>gdbm</em> with the key-value pairs of <em>other</em>. <em>other</em> must have an <a href="GDBM.html#method-i-each_pair"><code>each_pair</code></a> method.</p>
<div class="method-source-code" id="replace-source">
<pre>static VALUE
fgdbm_replace(VALUE obj, VALUE other)
{
fgdbm_clear(obj);
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-select" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
select { |key, value| block } → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a new array of all key-value pairs of the database for which <em>block</em> evaluates to true.</p>
<div class="method-source-code" id="select-source">
<pre>static VALUE
fgdbm_select(VALUE obj)
{
VALUE new = rb_ary_new();
GDBM_FILE dbm;
struct dbmdata *dbmp;
VALUE keystr;
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
VALUE v = rb_yield(assoc);
if (RTEST(v)) {
rb_ary_push(new, assoc);
}
GetDBM2(obj, dbmp, dbm);
}
return new;
}</pre>
</div>
</div>
</div>
<div id="method-i-shift" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
shift → (key, value) or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Removes a key-value-pair from this database and returns it as a two-item array [ <em>key</em>, <em>value</em> ]. Returns nil if the database is empty.</p>
<div class="method-source-code" id="shift-source">
<pre>static VALUE
fgdbm_shift(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
keystr = rb_gdbm_firstkey(dbm);
if (NIL_P(keystr)) return Qnil;
valstr = rb_gdbm_fetch2(dbm, keystr);
rb_gdbm_delete(obj, keystr);
return rb_assoc_new(keystr, valstr);
}</pre>
</div>
</div>
</div>
<div id="method-i-size" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
size → fixnum
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the number of key-value pairs in this database.</p>
<div class="method-source-code" id="size-source">
<pre>static VALUE
fgdbm_length(VALUE obj)
{
datum key, nextkey;
struct dbmdata *dbmp;
GDBM_FILE dbm;
int i = 0;
GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
free(key.dptr);
i++;
}
dbmp->di_size = i;
return INT2FIX(i);
}</pre>
</div>
</div>
</div>
<div id="method-i-store" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
store(key, value) → value
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Associates the value <em>value</em> with the specified <em>key</em>.</p>
<div class="method-source-code" id="store-source">
<pre>static VALUE
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
GDBM_FILE dbm;
rb_gdbm_modify(obj);
ExportStringValue(keystr);
ExportStringValue(valstr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
val.dptr = RSTRING_PTR(valstr);
val.dsize = RSTRING_LENINT(valstr);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
if (errno == EPERM) rb_sys_fail(0);
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
return valstr;
}</pre>
</div>
</div>
</div>
<div id="method-i-sync" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sync → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Unless the <em>gdbm</em> object has been opened with the <strong>SYNC</strong> flag, it is not guaranteed that database modification operations are immediately applied to the database file. This method ensures that all recent modifications to the database are written to the file. Blocks until all writing operations to the disk have been finished.</p>
<div class="method-source-code" id="sync-source">
<pre>static VALUE
fgdbm_sync(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
rb_gdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
gdbm_sync(dbm);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-syncmode-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
syncmode = boolean → boolean
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Turns the database's synchronization mode on or off. If the synchronization mode is turned on, the database's in-memory state will be synchronized to disk after every database modification operation. If the synchronization mode is turned off, <a href="GDBM.html"><code>GDBM</code></a> does not wait for writes to be flushed to the disk before continuing.</p>
<p>This option is only available for gdbm >= 1.8 where syncmode is turned off by default. See also: <a href="GDBM.html#method-i-fastmode-3D"><code>fastmode=</code></a></p>
<div class="method-source-code" id="syncmode-3D-source">
<pre>static VALUE
fgdbm_set_syncmode(VALUE obj, VALUE val)
{
#if !defined(GDBM_SYNCMODE)
fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
return val;
#else
struct dbmdata *dbmp;
GDBM_FILE dbm;
int optval;
GetDBM2(obj, dbmp, dbm);
optval = 0;
if (RTEST(val))
optval = 1;
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
return val;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-i-to_a" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
to_a → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of all key-value pairs contained in the database.</p>
<div class="method-source-code" id="to_a-source">
<pre>static VALUE
fgdbm_to_a(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
}
return ary;
}</pre>
</div>
</div>
</div>
<div id="method-i-to_hash" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
to_hash → hash
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a hash of all key-value pairs contained in the database.</p>
<div class="method-source-code" id="to_hash-source">
<pre>static VALUE
fgdbm_to_hash(VALUE obj)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, hash;
GetDBM2(obj, dbmp, dbm);
hash = rb_hash_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
}
return hash;
}</pre>
</div>
</div>
</div>
<div id="method-i-update" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
update(other) → gdbm
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Adds the key-value pairs of <em>other</em> to <em>gdbm</em>, overwriting entries with duplicate keys with those from <em>other</em>. <em>other</em> must have an <a href="GDBM.html#method-i-each_pair"><code>each_pair</code></a> method.</p>
<div class="method-source-code" id="update-source">
<pre>static VALUE
fgdbm_update(VALUE obj, VALUE other)
{
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-value-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
value?(v) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the given value <em>v</em> exists within the database. Returns false otherwise.</p>
<div class="method-source-code" id="value-3F-source">
<pre>static VALUE
fgdbm_has_value(VALUE obj, VALUE valstr)
{
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE keystr, valstr2;
ExportStringValue(valstr);
GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr2 = rb_gdbm_fetch2(dbm, keystr);
if (!NIL_P(valstr2) &&
(int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
(int)RSTRING_LEN(valstr)) == 0) {
return Qtrue;
}
}
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-values" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
values → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of all values of this database.</p>
<div class="method-source-code" id="values-source">
<pre>static VALUE
fgdbm_values(VALUE obj)
{
datum key, nextkey;
struct dbmdata *dbmp;
GDBM_FILE dbm;
VALUE valstr, ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
valstr = rb_gdbm_fetch(dbm, key);
free(key.dptr);
rb_ary_push(ary, valstr);
}
return ary;
}</pre>
</div>
</div>
</div>
<div id="method-i-values_at" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
values_at(key, ...) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of the values associated with each specified <em>key</em>.</p>
<div class="method-source-code" id="values_at-source">
<pre>static VALUE
fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
{
VALUE new = rb_ary_new2(argc);
int i;
for (i=0; i<argc; i++) {
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
}
return new;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>