HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/SDBM.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>class SDBM - 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 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-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-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-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-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-SDBM">
  <h1 id="class-SDBM" class="class">
    class SDBM
  </h1>

  <section class="description">
    
<p><a href="SDBM.html"><code>SDBM</code></a> provides a simple file-based key-value store, which can only store <a href="String.html"><code>String</code></a> keys and values.</p>

<p>Note that Ruby comes with the source code for <a href="SDBM.html"><code>SDBM</code></a>, while the <a href="DBM.html"><code>DBM</code></a> and <a href="GDBM.html"><code>GDBM</code></a> standard libraries rely on external libraries and headers.</p>

<h3 id="class-SDBM-label-Examples">Examples<span><a href="#class-SDBM-label-Examples">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Insert values:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;apple&#39;</span>] = <span class="ruby-string">&#39;fruit&#39;</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;pear&#39;</span>] = <span class="ruby-string">&#39;fruit&#39;</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;carrot&#39;</span>] = <span class="ruby-string">&#39;vegetable&#39;</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;tomato&#39;</span>] = <span class="ruby-string">&#39;vegetable&#39;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Bulk update:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">update</span>(<span class="ruby-string">&#39;peach&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;fruit&#39;</span>, <span class="ruby-string">&#39;tomato&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;fruit&#39;</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>Retrieve values:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">each</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">puts</span> <span class="ruby-node">&quot;Key: #{key}, Value: #{value}&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Outputs:</p>

<pre>Key: apple, Value: fruit
Key: pear, Value: fruit
Key: carrot, Value: vegetable
Key: peach, Value: fruit
Key: tomato, Value: fruit</pre>

  </section>

  
  <section id="5Buntitled-5D" class="documentation-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)
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Creates a new database handle by opening the given <code>filename</code>. <a href="SDBM.html"><code>SDBM</code></a> actually uses two physical files, with extensions &#39;.dir&#39; and &#39;.pag&#39;. These extensions will automatically be appended to the <code>filename</code>.</p>

<p>If the file does not exist, a new file will be created using the given <code>mode</code>, unless <code>mode</code> is explicitly set to nil. In the latter case, no database will be created.</p>

<p>If the file exists, it will be opened in read/write mode. If this fails, it will be opened in read-only mode.</p>
          
          

          
          <div class="method-source-code" id="new-source">
            <pre>static VALUE
fsdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
    VALUE file, vmode;
    DBM *dbm;
    struct dbmdata *dbmp;
    int mode;

    TypedData_Get_Struct(obj, struct dbmdata, &amp;sdbm_type, dbmp);
    if (rb_scan_args(argc, argv, &quot;11&quot;, &amp;file, &amp;vmode) == 1) {
        mode = 0666;           /* default value */
    }
    else if (NIL_P(vmode)) {
        mode = -1;             /* return nil if DB not exist */
    }
    else {
        mode = NUM2INT(vmode);
    }
    FilePathValue(file);

    dbm = 0;
    if (mode &gt;= 0)
        dbm = sdbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
    if (!dbm)
        dbm = sdbm_open(RSTRING_PTR(file), O_RDWR, 0);
    if (!dbm)
        dbm = sdbm_open(RSTRING_PTR(file), O_RDONLY, 0);

    if (!dbm) {
        if (mode == -1) return Qnil;
        rb_sys_fail_str(file);
    }

    if (dbmp-&gt;di_dbm)
        sdbm_close(dbmp-&gt;di_dbm);
    dbmp-&gt;di_dbm = dbm;
    dbmp-&gt;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)
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            open(filename, mode = 0666) { |sdbm| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>If called without a block, this is the same as <a href="SDBM.html#method-c-new"><code>SDBM.new</code></a>.</p>

<p>If a block is given, the new database will be passed to the block and will be safely closed after the block has executed.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;my_database&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;hello&#39;</span>] = <span class="ruby-string">&#39;world&#39;</span>
<span class="ruby-keyword">end</span>
</pre>
          
          

          
          <div class="method-source-code" id="open-source">
            <pre>static VALUE
fsdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = fsdbm_alloc(klass);

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

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, obj, fsdbm_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">
            sdbm[key] &rarr; value or nil
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the <code>value</code> in the database associated with the given <code>key</code> string.</p>

<p>If no value is found, returns <code>nil</code>.</p>
          
          

          
          <div class="method-source-code" id="5B-5D-source">
            <pre>static VALUE
fsdbm_aref(VALUE obj, VALUE keystr)
{
    return fsdbm_fetch(obj, keystr, Qnil);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-5B-5D-3D" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            sdbm[key] = value      &rarr; value
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Stores a new <code>value</code> in the database with the given <code>key</code> as an index.</p>

<p>If the <code>key</code> already exists, this will update the <code>value</code> associated with the <code>key</code>.</p>

<p>Returns the given <code>value</code>.</p>
          
          

          
          <div class="method-source-code" id="5B-5D-3D-source">
            <pre>static VALUE
fsdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    if (valstr == Qnil) {
        fsdbm_delete(obj, keystr);
        return Qnil;
    }

    fdbm_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-&gt;di_size = -1;
    if (sdbm_store(dbm, key, val, DBM_REPLACE)) {
#ifdef HAVE_DBM_CLAERERR
        sdbm_clearerr(dbm);
#endif
        if (errno == EPERM) rb_sys_fail(0);
        rb_raise(rb_eDBMError, &quot;sdbm_store failed&quot;);
    }

    return valstr;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-clear" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            clear &rarr; self
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Deletes all data from the database.</p>
          
          

          
          <div class="method-source-code" id="clear-source">
            <pre>static VALUE
fsdbm_clear(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;

    fdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    dbmp-&gt;di_size = -1;
    while (key = sdbm_firstkey(dbm), key.dptr) {
        if (sdbm_delete(dbm, key)) {
            rb_raise(rb_eDBMError, &quot;sdbm_delete failed&quot;);
        }
    }
    dbmp-&gt;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 &rarr; nil
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Closes the database file.</p>

<p>Raises <a href="SDBMError.html"><code>SDBMError</code></a> if the database is already closed.</p>
          
          

          
          <div class="method-source-code" id="close-source">
            <pre>static VALUE
fsdbm_close(VALUE obj)
{
    struct dbmdata *dbmp;

    GetDBM(obj, dbmp);
    sdbm_close(dbmp-&gt;di_dbm);
    dbmp-&gt;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? &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database is closed.</p>
          
          

          
          <div class="method-source-code" id="closed-3F-source">
            <pre>static VALUE
fsdbm_closed(VALUE obj)
{
    struct dbmdata *dbmp;

    TypedData_Get_Struct(obj, struct dbmdata, &amp;sdbm_type, dbmp);
    if (dbmp-&gt;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) &rarr; value or nil
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            delete(key) { |key, value| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Deletes the key-value pair corresponding to the given <code>key</code>. If the <code>key</code> exists, the deleted value will be returned, otherwise <code>nil</code>.</p>

<p>If a block is provided, the deleted <code>key</code> and <code>value</code> will be passed to the block as arguments. If the <code>key</code> does not exist in the database, the value will be <code>nil</code>.</p>
          
          

          
          <div class="method-source-code" id="delete-source">
            <pre>static VALUE
fsdbm_delete(VALUE obj, VALUE keystr)
{
    datum key, value;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE valstr;

    fdbm_modify(obj);
    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    GetDBM2(obj, dbmp, dbm);
    dbmp-&gt;di_size = -1;

    value = sdbm_fetch(dbm, key);
    if (value.dptr == 0) {
        if (rb_block_given_p()) return rb_yield(keystr);
        return Qnil;
    }

    /* need to save value before sdbm_delete() */
    valstr = rb_external_str_new(value.dptr, value.dsize);

    if (sdbm_delete(dbm, key)) {
        dbmp-&gt;di_size = -1;
        rb_raise(rb_eDBMError, &quot;dbm_delete failed&quot;);
    }
    else if (dbmp-&gt;di_size &gt;= 0) {
        dbmp-&gt;di_size--;
    }
    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| ... } &rarr; self
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over the key-value pairs in the database, deleting those for which the block returns <code>true</code>.</p>
          
          

          
          <div class="method-source-code" id="delete_if-source">
            <pre>static VALUE
fsdbm_delete_if(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE keystr, valstr;
    VALUE ret, ary = rb_ary_new();
    long i;
    int status = 0, n;

    fdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    n = dbmp-&gt;di_size;
    dbmp-&gt;di_size = -1;
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        keystr = rb_external_str_new(key.dptr, key.dsize);
        valstr = rb_external_str_new(val.dptr, val.dsize);
        ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &amp;status);
        if (status != 0) break;
        if (RTEST(ret)) rb_ary_push(ary, keystr);
        GetDBM2(obj, dbmp, dbm);
    }

    for (i = 0; i &lt; RARRAY_LEN(ary); i++) {
        keystr = RARRAY_AREF(ary, i);
        ExportStringValue(keystr);
        key.dptr = RSTRING_PTR(keystr);
        key.dsize = RSTRING_LENINT(keystr);
        if (sdbm_delete(dbm, key)) {
            rb_raise(rb_eDBMError, &quot;sdbm_delete failed&quot;);
        }
    }
    if (status) rb_jump_tag(status);
    if (n &gt; 0) dbmp-&gt;di_size = n - RARRAY_LENINT(ary);

    return obj;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-each" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            each
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each { |key, value| ... }
          </span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each_pair
          </span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each_pair { |key, value| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over each key-value pair in the database.</p>

<p>If no block is given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>
          
          

          
          <div class="method-source-code" id="each-source">
            <pre>static VALUE
fsdbm_each_pair(VALUE obj)
{
    datum key, val;
    DBM *dbm;
    struct dbmdata *dbmp;
    VALUE keystr, valstr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        keystr = rb_external_str_new(key.dptr, key.dsize);
        valstr = rb_external_str_new(val.dptr, val.dsize);
        rb_yield(rb_assoc_new(keystr, valstr));
        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
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each_key { |key| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over each <code>key</code> in the database.</p>

<p>If no block is given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>
          
          

          
          <div class="method-source-code" id="each_key-source">
            <pre>static VALUE
fsdbm_each_key(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        rb_yield(rb_external_str_new(key.dptr, key.dsize));
        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
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each_pair { |key, value| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over each key-value pair in the database.</p>

<p>If no block is given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>
          
          

          
          <div class="method-source-code" id="each_pair-source">
            <pre>static VALUE
fsdbm_each_pair(VALUE obj)
{
    datum key, val;
    DBM *dbm;
    struct dbmdata *dbmp;
    VALUE keystr, valstr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        keystr = rb_external_str_new(key.dptr, key.dsize);
        valstr = rb_external_str_new(val.dptr, val.dsize);
        rb_yield(rb_assoc_new(keystr, valstr));
        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
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            each_value { |value| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over each <code>value</code> in the database.</p>

<p>If no block is given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>
          
          

          
          <div class="method-source-code" id="each_value-source">
            <pre>static VALUE
fsdbm_each_value(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        rb_yield(rb_external_str_new(val.dptr, val.dsize));
        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? &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database is empty.</p>
          
          

          
          <div class="method-source-code" id="empty-3F-source">
            <pre>static VALUE
fsdbm_empty_p(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;

    GetDBM(obj, dbmp);
    if (dbmp-&gt;di_size &lt; 0) {
        dbm = dbmp-&gt;di_dbm;

        for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
            return Qfalse;
        }
    }
    else {
        if (dbmp-&gt;di_size)
            return Qfalse;
    }
    return Qtrue;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-fetch" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            fetch(key) &rarr; value or nil
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        <div class="method-heading">
          <span class="method-callseq">
            fetch(key) { |key| ... }
          </span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the <code>value</code> in the database associated with the given <code>key</code> string.</p>

<p>If a block is provided, the block will be called when there is no <code>value</code> associated with the given <code>key</code>. The <code>key</code> will be passed in as an argument to the block.</p>

<p>If no block is provided and no value is associated with the given <code>key</code>, then an <a href="IndexError.html"><code>IndexError</code></a> will be raised.</p>
          
          

          
          <div class="method-source-code" id="fetch-source">
            <pre>static VALUE
fsdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
{
    VALUE keystr, valstr, ifnone;

    rb_scan_args(argc, argv, &quot;11&quot;, &amp;keystr, &amp;ifnone);
    valstr = fsdbm_fetch(obj, keystr, ifnone);
    if (argc == 1 &amp;&amp; !rb_block_given_p() &amp;&amp; NIL_P(valstr))
        rb_raise(rb_eIndexError, &quot;key not found&quot;);

    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?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="has_key-3F-source">
            <pre>static VALUE
fsdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    GetDBM2(obj, dbmp, dbm);
    val = sdbm_fetch(dbm, key);
    if (val.dptr) 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?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>value</code>.</p>
          
          

          
          <div class="method-source-code" id="has_value-3F-source">
            <pre>static VALUE
fsdbm_has_value(VALUE obj, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(valstr);
    val.dptr = RSTRING_PTR(valstr);
    val.dsize = RSTRING_LENINT(valstr);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        if (val.dsize == RSTRING_LENINT(valstr) &amp;&amp;
            memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 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?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="include-3F-source">
            <pre>static VALUE
fsdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    GetDBM2(obj, dbmp, dbm);
    val = sdbm_fetch(dbm, key);
    if (val.dptr) 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 &rarr; Hash
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a <a href="Hash.html"><code>Hash</code></a> in which the key-value pairs have been inverted.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">update</span>(<span class="ruby-string">&#39;apple&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;fruit&#39;</span>, <span class="ruby-string">&#39;spinach&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;vegetable&#39;</span>)

  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">invert</span>  <span class="ruby-comment">#=&gt; {&quot;fruit&quot; =&gt; &quot;apple&quot;, &quot;vegetable&quot; =&gt; &quot;spinach&quot;}</span>
<span class="ruby-keyword">end</span>
</pre>
          
          

          
          <div class="method-source-code" id="invert-source">
            <pre>static VALUE
fsdbm_invert(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE keystr, valstr;
    VALUE hash = rb_hash_new();

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        keystr = rb_external_str_new(key.dptr, key.dsize);
        valstr = rb_external_str_new(val.dptr, val.dsize);
        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) &rarr; key
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the <code>key</code> associated with the given <code>value</code>. If more than one <code>key</code> corresponds to the given <code>value</code>, then the first key to be found will be returned. If no keys are found, <code>nil</code> will be returned.</p>
          
          

          
          <div class="method-source-code" id="key-source">
            <pre>static VALUE
fsdbm_key(VALUE obj, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(valstr);
    val.dptr = RSTRING_PTR(valstr);
    val.dsize = RSTRING_LENINT(valstr);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        if (val.dsize == RSTRING_LEN(valstr) &amp;&amp;
            memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
            return rb_external_str_new(key.dptr, key.dsize);
    }
    return Qnil;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-key-3F" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            key?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="key-3F-source">
            <pre>static VALUE
fsdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    GetDBM2(obj, dbmp, dbm);
    val = sdbm_fetch(dbm, key);
    if (val.dptr) 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 &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a new <a href="Array.html"><code>Array</code></a> containing the keys in the database.</p>
          
          

          
          <div class="method-source-code" id="keys-source">
            <pre>static VALUE
fsdbm_keys(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        rb_ary_push(ary, rb_external_str_new(key.dptr, key.dsize));
    }

    return ary;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-length" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            length &rarr; integer
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the number of keys in the database.</p>
          
          

          
          <div class="method-source-code" id="length-source">
            <pre>static VALUE
fsdbm_length(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;
    int i = 0;

    GetDBM2(obj, dbmp, dbm);
    if (dbmp-&gt;di_size &gt; 0) return INT2FIX(dbmp-&gt;di_size);

    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        i++;
    }
    dbmp-&gt;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?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="member-3F-source">
            <pre>static VALUE
fsdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    GetDBM2(obj, dbmp, dbm);
    val = sdbm_fetch(dbm, key);
    if (val.dptr) 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| ... } &rarr; Hash
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Creates a new <a href="Hash.html"><code>Hash</code></a> using the key-value pairs from the database, then calls <a href="Hash.html#method-i-reject"><code>Hash#reject</code></a> with the given block, which returns a <a href="Hash.html"><code>Hash</code></a> with only the key-value pairs for which the block returns <code>false</code>.</p>
          
          

          
          <div class="method-source-code" id="reject-source">
            <pre>static VALUE
fsdbm_reject(VALUE obj)
{
    return rb_hash_delete_if(fsdbm_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| ... } &rarr; self
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Iterates over the key-value pairs in the database, deleting those for which the block returns <code>true</code>.</p>
          
          

          
          <div class="method-source-code" id="reject-21-source">
            <pre>static VALUE
fsdbm_delete_if(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE keystr, valstr;
    VALUE ret, ary = rb_ary_new();
    long i;
    int status = 0, n;

    fdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    n = dbmp-&gt;di_size;
    dbmp-&gt;di_size = -1;
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        keystr = rb_external_str_new(key.dptr, key.dsize);
        valstr = rb_external_str_new(val.dptr, val.dsize);
        ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &amp;status);
        if (status != 0) break;
        if (RTEST(ret)) rb_ary_push(ary, keystr);
        GetDBM2(obj, dbmp, dbm);
    }

    for (i = 0; i &lt; RARRAY_LEN(ary); i++) {
        keystr = RARRAY_AREF(ary, i);
        ExportStringValue(keystr);
        key.dptr = RSTRING_PTR(keystr);
        key.dsize = RSTRING_LENINT(keystr);
        if (sdbm_delete(dbm, key)) {
            rb_raise(rb_eDBMError, &quot;sdbm_delete failed&quot;);
        }
    }
    if (status) rb_jump_tag(status);
    if (n &gt; 0) dbmp-&gt;di_size = n - RARRAY_LENINT(ary);

    return obj;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-replace" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            replace(pairs) &rarr; self
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Empties the database, then inserts the given key-value pairs.</p>

<p>This method will work with any object which implements an <a href="SDBM.html#method-i-each_pair"><code>each_pair</code></a> method, such as a <a href="Hash.html"><code>Hash</code></a>.</p>
          
          

          
          <div class="method-source-code" id="replace-source">
            <pre>static VALUE
fsdbm_replace(VALUE obj, VALUE other)
{
    fsdbm_clear(obj);
    rb_block_call(other, rb_intern(&quot;each_pair&quot;), 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| ... } &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a new <a href="Array.html"><code>Array</code></a> of key-value pairs for which the block returns <code>true</code>.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;apple&#39;</span>] = <span class="ruby-string">&#39;fruit&#39;</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;pear&#39;</span>] = <span class="ruby-string">&#39;fruit&#39;</span>
  <span class="ruby-identifier">db</span>[<span class="ruby-string">&#39;spinach&#39;</span>] = <span class="ruby-string">&#39;vegetable&#39;</span>

  <span class="ruby-identifier">veggies</span> = <span class="ruby-identifier">db</span>.<span class="ruby-identifier">select</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">value</span> <span class="ruby-operator">==</span> <span class="ruby-string">&#39;vegetable&#39;</span>
  <span class="ruby-keyword">end</span> <span class="ruby-comment">#=&gt; [[&quot;apple&quot;, &quot;fruit&quot;], [&quot;pear&quot;, &quot;fruit&quot;]]</span>
<span class="ruby-keyword">end</span>
</pre>
          
          

          
          <div class="method-source-code" id="select-source">
            <pre>static VALUE
fsdbm_select(VALUE obj)
{
    VALUE new = rb_ary_new();
    datum key, val;
    DBM *dbm;
    struct dbmdata *dbmp;

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        VALUE assoc, v;
        val = sdbm_fetch(dbm, key);
        assoc = rb_assoc_new(rb_external_str_new(key.dptr, key.dsize),
                             rb_external_str_new(val.dptr, val.dsize));
        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 &rarr; Array 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 the database and returns them as an <a href="Array.html"><code>Array</code></a>. If the database is empty, returns <code>nil</code>.</p>
          
          

          
          <div class="method-source-code" id="shift-source">
            <pre>static VALUE
fsdbm_shift(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE keystr, valstr;

    fdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    key = sdbm_firstkey(dbm);
    if (!key.dptr) return Qnil;
    val = sdbm_fetch(dbm, key);
    keystr = rb_external_str_new(key.dptr, key.dsize);
    valstr = rb_external_str_new(val.dptr, val.dsize);
    sdbm_delete(dbm, key);
    if (dbmp-&gt;di_size &gt;= 0) {
        dbmp-&gt;di_size--;
    }

    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 &rarr; integer
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the number of keys in the database.</p>
          
          

          
          <div class="method-source-code" id="size-source">
            <pre>static VALUE
fsdbm_length(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    DBM *dbm;
    int i = 0;

    GetDBM2(obj, dbmp, dbm);
    if (dbmp-&gt;di_size &gt; 0) return INT2FIX(dbmp-&gt;di_size);

    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        i++;
    }
    dbmp-&gt;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) &rarr; value
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Stores a new <code>value</code> in the database with the given <code>key</code> as an index.</p>

<p>If the <code>key</code> already exists, this will update the <code>value</code> associated with the <code>key</code>.</p>

<p>Returns the given <code>value</code>.</p>
          
          

          
          <div class="method-source-code" id="store-source">
            <pre>static VALUE
fsdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    if (valstr == Qnil) {
        fsdbm_delete(obj, keystr);
        return Qnil;
    }

    fdbm_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-&gt;di_size = -1;
    if (sdbm_store(dbm, key, val, DBM_REPLACE)) {
#ifdef HAVE_DBM_CLAERERR
        sdbm_clearerr(dbm);
#endif
        if (errno == EPERM) rb_sys_fail(0);
        rb_raise(rb_eDBMError, &quot;sdbm_store failed&quot;);
    }

    return valstr;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-to_a" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            to_a &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a new <a href="Array.html"><code>Array</code></a> containing each key-value pair in the database.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;sdbm&#39;</span>

<span class="ruby-constant">SDBM</span>.<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;my_database&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">db</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">update</span>(<span class="ruby-string">&#39;apple&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;fruit&#39;</span>, <span class="ruby-string">&#39;spinach&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;vegetable&#39;</span>)

  <span class="ruby-identifier">db</span>.<span class="ruby-identifier">to_a</span>  <span class="ruby-comment">#=&gt; [[&quot;apple&quot;, &quot;fruit&quot;], [&quot;spinach&quot;, &quot;vegetable&quot;]]</span>
<span class="ruby-keyword">end</span>
</pre>
          
          

          
          <div class="method-source-code" id="to_a-source">
            <pre>static VALUE
fsdbm_to_a(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        rb_ary_push(ary, rb_assoc_new(rb_external_str_new(key.dptr, key.dsize),
                                      rb_external_str_new(val.dptr, val.dsize)));
    }

    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 &rarr; Hash
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a new <a href="Hash.html"><code>Hash</code></a> containing each key-value pair in the database.</p>
          
          

          
          <div class="method-source-code" id="to_hash-source">
            <pre>static VALUE
fsdbm_to_hash(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE hash;

    GetDBM2(obj, dbmp, dbm);
    hash = rb_hash_new();
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        rb_hash_aset(hash, rb_external_str_new(key.dptr, key.dsize),
                           rb_external_str_new(val.dptr, val.dsize));
    }

    return hash;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-update" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            update(pairs) &rarr; self
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Insert or update key-value pairs.</p>

<p>This method will work with any object which implements an <a href="SDBM.html#method-i-each_pair"><code>each_pair</code></a> method, such as a <a href="Hash.html"><code>Hash</code></a>.</p>
          
          

          
          <div class="method-source-code" id="update-source">
            <pre>static VALUE
fsdbm_update(VALUE obj, VALUE other)
{
    rb_block_call(other, rb_intern(&quot;each_pair&quot;), 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?(key) &rarr; true or false
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if the database contains the given <code>value</code>.</p>
          
          

          
          <div class="method-source-code" id="value-3F-source">
            <pre>static VALUE
fsdbm_has_value(VALUE obj, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(valstr);
    val.dptr = RSTRING_PTR(valstr);
    val.dsize = RSTRING_LENINT(valstr);

    GetDBM2(obj, dbmp, dbm);
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        if (val.dsize == RSTRING_LENINT(valstr) &amp;&amp;
            memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 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 &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns a new <a href="Array.html"><code>Array</code></a> containing the values in the database.</p>
          
          

          
          <div class="method-source-code" id="values-source">
            <pre>static VALUE
fsdbm_values(VALUE obj)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    VALUE ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
        val = sdbm_fetch(dbm, key);
        rb_ary_push(ary, rb_external_str_new(val.dptr, val.dsize));
    }

    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, ...) &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns an <a href="Array.html"><code>Array</code></a> of values corresponding to the given keys.</p>
          
          

          
          <div class="method-source-code" id="values_at-source">
            <pre>static VALUE
fsdbm_values_at(int argc, VALUE *argv, VALUE obj)
{
    VALUE new = rb_ary_new2(argc);
    int i;

    for (i=0; i&lt;argc; i++) {
        rb_ary_push(new, fsdbm_fetch(obj, argv[i], Qnil));
    }

    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>