Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Ruby

Write to file

1
2
3
4
5
# open and write to a file with ruby
file_path = File.expand_path("~/lotto.log")
File.open(file_path, 'w') { |f|
    f.puts "Log test."
}
https://snipt.net/embed/9d776f3929b949d45adcc14eb1de91d9/
https://snipt.net/raw/9d776f3929b949d45adcc14eb1de91d9/
9d776f3929b949d45adcc14eb1de91d9
rb
Ruby
5
2016-12-10T17:07:55
True
False
False
/api/public/snipt/40830/
write-to-file
<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="c1"># open and write to a file with ruby</span> </span><span id="L-2"><a name="L-2"></a><span class="n">file_path</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">expand_path</span><span class="p">(</span><span class="s2">&quot;~/lotto.log&quot;</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">f</span><span class="o">.</span><span class="n">puts</span> <span class="s2">&quot;Log test.&quot;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
file, io

C#

Search files in a folder by multiple file types

"*.txt|*.doc".Split('|').SelectMany(filter => System.IO.Directory.GetFiles(strFolderPath, filter)).ToArray()
https://snipt.net/embed/2c806b436178aea0859328bbde0acd75/
https://snipt.net/raw/2c806b436178aea0859328bbde0acd75/
2c806b436178aea0859328bbde0acd75
csharp
C#
1
2016-12-10T17:11:21
True
False
False
/api/public/snipt/31332/
search-files-in-a-folder-by-multiple-file-types
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="s">&quot;*.txt|*.doc&quot;</span><span class="p">.</span><span class="n">Split</span><span class="p">(</span><span class="sc">&#39;|&#39;</span><span class="p">).</span><span class="n">SelectMany</span><span class="p">(</span><span class="n">filter</span> <span class="p">=&gt;</span> <span class="n">System</span><span class="p">.</span><span class="n">IO</span><span class="p">.</span><span class="n">Directory</span><span class="p">.</span><span class="n">GetFiles</span><span class="p">(</span><span class="n">strFolderPath</span><span class="p">,</span> <span class="n">filter</span><span class="p">)).</span><span class="n">ToArray</span><span class="p">()</span> </span></pre></div> </td></tr></table>
"file extensions", "search pattern", io, search

Scala

Read entire file in Scala

1
2
3
//http://stackoverflow.com/questions/1284423/read-entire-file-in-scala

val lines = scala.io.Source.fromFile("file.txt").mkString
https://snipt.net/embed/aa71bdb5f7d77c281397fa9f04212219/
https://snipt.net/raw/aa71bdb5f7d77c281397fa9f04212219/
aa71bdb5f7d77c281397fa9f04212219
scala
Scala
3
2016-12-09T17:48:09
True
False
False
/api/public/snipt/28096/
read-entire-file-in-scala
<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="c1">//http://stackoverflow.com/questions/1284423/read-entire-file-in-scala</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">val</span> <span class="n">lines</span> <span class="k">=</span> <span class="n">scala</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="nc">Source</span><span class="o">.</span><span class="n">fromFile</span><span class="o">(</span><span class="s">&quot;file.txt&quot;</span><span class="o">).</span><span class="n">mkString</span> </span></pre></div> </td></tr></table>
file, io, scala
Copyrighted, illegal, or inappropriate content? Email [email protected].