snipt

Ctrl+h for KB shortcuts

Ruby

String to UUID format (Ruby)

1
2
3
4
5
6
7
class String
  def to_uuid_format
    s = self.gsub(/[^a-fA-F0-9]/, '')
    s = s + ("0" * (32 - s.length))
    "#{s[0..7]}-#{s[8..11]}-#{s[12..15]}-#{s[16..19]}-#{s[20..31]}"
  end
end
https://snipt.net/embed/e8ccc668e24bcf379167db80ec03f91f/
/raw/e8ccc668e24bcf379167db80ec03f91f/
e8ccc668e24bcf379167db80ec03f91f
rb
Ruby
8
2019-07-18T01:34:43
True
False
False
Sep 09, 2016 at 03:19 PM
/api/public/snipt/147728/
string-to-uuid-format-ruby
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">class</span> <span class="nc">String</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">def</span> <span class="nf">to_uuid_format</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">s</span> <span class="o">=</span> <span class="nb">self</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/[^a-fA-F0-9]/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">s</span> <span class="o">=</span> <span class="n">s</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;0&quot;</span> <span class="o">*</span> <span class="p">(</span><span class="mi">32</span> <span class="o">-</span> <span class="n">s</span><span class="o">.</span><span class="n">length</span><span class="p">))</span> </span><span id="L-5"><a name="L-5"></a> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">s</span><span class="o">[</span><span class="mi">0</span><span class="o">..</span><span class="mi">7</span><span class="o">]</span><span class="si">}</span><span class="s2">-</span><span class="si">#{</span><span class="n">s</span><span class="o">[</span><span class="mi">8</span><span class="o">..</span><span class="mi">11</span><span class="o">]</span><span class="si">}</span><span class="s2">-</span><span class="si">#{</span><span class="n">s</span><span class="o">[</span><span class="mi">12</span><span class="o">..</span><span class="mi">15</span><span class="o">]</span><span class="si">}</span><span class="s2">-</span><span class="si">#{</span><span class="n">s</span><span class="o">[</span><span class="mi">16</span><span class="o">..</span><span class="mi">19</span><span class="o">]</span><span class="si">}</span><span class="s2">-</span><span class="si">#{</span><span class="n">s</span><span class="o">[</span><span class="mi">20</span><span class="o">..</span><span class="mi">31</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">end</span> </span><span id="L-7"><a name="L-7"></a><span class="k">end</span> </span></pre></div> </td></tr></table>
--- 
+++ 
@@ -0,0 +1,7 @@
+class String
+  def to_uuid_format
+    s = self.gsub(/[^a-fA-F0-9]/, '')
+    s = s + ("0" * (32 - s.length))
+    "#{s[0..7]}-#{s[8..11]}-#{s[12..15]}-#{s[16..19]}-#{s[20..31]}"
+  end
+end