Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

JavaScript

Trim string to length

1
2
3
4
5
function trimLength(str, length) {
	str = $.trim(str); // jQuery required
	str = str.slice(0, length);
	return str;
}
https://snipt.net/embed/b282ce90eccedb80413e42bfea8f0572/
https://snipt.net/raw/b282ce90eccedb80413e42bfea8f0572/
b282ce90eccedb80413e42bfea8f0572
js
JavaScript
5
2016-12-09T11:17:32
True
False
False
/api/public/snipt/23868/
trim-string-to-length
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">trimLength</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">length</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">str</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">trim</span><span class="p">(</span><span class="nx">str</span><span class="p">);</span> <span class="c1">// jQuery required</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">str</span> <span class="o">=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">length</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="nx">str</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
cut, function, helper, javascript, length, shorten, snippet, string, trim

Smalltalk

Helper function to assert non-nil return

1
2
3
Object>assertNotNil
    self assert: self notNil.
    ^ self
https://snipt.net/embed/76bd1665a1176b6af1e1e4c4fcb389f6/
https://snipt.net/raw/76bd1665a1176b6af1e1e4c4fcb389f6/
76bd1665a1176b6af1e1e4c4fcb389f6
smalltalk
Smalltalk
3
2016-12-09T05:44:11
True
False
False
/api/public/snipt/17569/
helper-function-to-assert-non-nil-return
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nc">Object</span><span class="nf">&gt;</span><span class="nv">assertNotNil</span> </span><span id="L-2"><a name="L-2"></a> <span class="nf">self</span> <span class="nf">assert:</span> <span class="bp">self</span> <span class="nf">notNil</span><span class="p">.</span> </span><span id="L-3"><a name="L-3"></a> <span class="o">^</span> <span class="bp">self</span> </span></pre></div> </td></tr></table>
"not nil", helper, smalltalk

Ruby

Link Helper

1
2
3
4
def make_links( str )
  regex = Regexp.new '(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)'
  str.gsub( regex, '<a href="\1">\1</a>' )
end
https://snipt.net/embed/fe560eeb29e3db2a702b225918f0d76f/
https://snipt.net/raw/fe560eeb29e3db2a702b225918f0d76f/
fe560eeb29e3db2a702b225918f0d76f
rb
Ruby
4
2016-12-06T02:01:47
True
False
False
/api/public/snipt/3334/
link-helper
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">make_links</span><span class="p">(</span> <span class="n">str</span> <span class="p">)</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">regex</span> <span class="o">=</span> <span class="no">Regexp</span><span class="o">.</span><span class="n">new</span> <span class="s1">&#39;(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)&#39;</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">str</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span> <span class="n">regex</span><span class="p">,</span> <span class="s1">&#39;&lt;a href=&quot;\1&quot;&gt;\1&lt;/a&gt;&#39;</span> <span class="p">)</span> </span><span id="L-4"><a name="L-4"></a><span class="k">end</span> </span></pre></div> </td></tr></table>
helper, regex, ruby
Copyrighted, illegal, or inappropriate content? Email [email protected].