Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Site updated at 2014-01-30 22:29:03 UTC

  • Loading branch information...
commit 0a39393d89403dd8629fc16c69246637f31c6adb 1 parent d07964b
@nikhgupta authored
View
197 atom.xml
@@ -4,7 +4,7 @@
<title><![CDATA[Nikhil Gupta]]></title>
<link href="http://nikhgupta.com/atom.xml" rel="self"/>
<link href="http://nikhgupta.com/"/>
- <updated>2014-01-02T21:03:57+05:30</updated>
+ <updated>2014-01-31T03:58:52+05:30</updated>
<id>http://nikhgupta.com/</id>
<author>
<name><![CDATA[Nikhil Gupta]]></name>
@@ -14,6 +14,201 @@
<entry>
+ <title type="html"><![CDATA[wrapping long lines in ruby for display in source files]]></title>
+ <link href="http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/"/>
+ <updated>2014-01-31T02:42:00+05:30</updated>
+ <id>http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files</id>
+ <content type="html"><![CDATA[<p>While working on a gem, I needed to generate ruby classes on the fly. Yeah,
+I know this should never be the case, unless I needed around 600+ classes (I was
+converting schema.org schemas to ruby classes).</p>
+
+<p>Now, this required me to add descriptive text inside these classes, but the text
+was very long and I hated how the text overflowed the screen. I use vim, which
+promptly highlights the 80-char column marker on my source files, and I wanted
+this text to stick to that. Effectively, I wanted something like the following:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">MyModule</span>
+</span><span class='line'> <span class="k">class</span> <span class="nc">MyClass</span>
+</span><span class='line'> <span class="n">describes</span> <span class="s2">&quot;A really long text that respects the 80-char column marker and</span>
+</span><span class='line'><span class="s2"> wraps nicely when it exceeds this length.&quot;</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+
+
+<!-- more -->
+
+
+<p>I knew that Rails provided a nearly similar method with the name <code>word_wrap</code>,
+and I tweaked it a bit to this:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">String</span>
+</span><span class='line'> <span class="k">def</span> <span class="nf">wrap</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:width</span><span class="p">,</span> <span class="mi">76</span><span class="p">)</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">strip</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">collect</span> <span class="k">do</span> <span class="o">|</span><span class="n">line</span><span class="o">|</span>
+</span><span class='line'> <span class="n">line</span><span class="o">.</span><span class="n">length</span> <span class="o">&gt;</span> <span class="n">width</span> <span class="p">?</span> <span class="n">line</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/(.{1,</span><span class="si">#{</span><span class="n">width</span><span class="si">}</span><span class="sr">})(\s+|$)/</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">1</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span> <span class="p">:</span> <span class="n">line</span>
+</span><span class='line'> <span class="k">end</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:strip</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="k">def</span> <span class="nf">indent</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">spaces</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:spaces</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/^/</span><span class="p">,</span> <span class="n">spaces</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/^\s*$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="k">def</span> <span class="nf">indent_with_wrap</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">spaces</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:spaces</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:width</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">width</span> <span class="o">&gt;</span> <span class="n">spaces</span> <span class="p">?</span> <span class="n">width</span> <span class="o">-</span> <span class="n">spaces</span> <span class="p">:</span> <span class="mi">1</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">wrap</span><span class="p">(</span><span class="ss">width</span><span class="p">:</span> <span class="n">width</span><span class="p">)</span><span class="o">.</span><span class="n">indent</span><span class="p">(</span><span class="ss">spaces</span><span class="p">:</span> <span class="n">spaces</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+<p>The last method, i.e. <code>indent_with_wrap</code> is what I am interested in, really.
+Now, this might not be the best implementation for what I want, but it works and
+is self-explanatory to me.</p>
+
+<p>So, now, I can create my ERB template like this to get the desired results:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">module</span> <span class="o">&lt;</span><span class="sx">%= module_name %&gt;</span>
+</span><span class='line'><span class="sx"> class &lt;%=</span> <span class="n">class_name</span> <span class="sx">%&gt;</span>
+</span><span class='line'><span class="sx"> &lt;%= &quot;describes </span><span class="si">#{</span><span class="n">description</span><span class="si">}</span><span class="sx">&quot;.indent_with_wrap spaces: 4, width: 80 %&gt;</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+<h3>Specs</h3>
+
+<p>Although, not really required (and not fully implemented), some sample tests can
+be:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+<span class='line-number'>21</span>
+<span class='line-number'>22</span>
+<span class='line-number'>23</span>
+<span class='line-number'>24</span>
+<span class='line-number'>25</span>
+<span class='line-number'>26</span>
+<span class='line-number'>27</span>
+<span class='line-number'>28</span>
+<span class='line-number'>29</span>
+<span class='line-number'>30</span>
+<span class='line-number'>31</span>
+<span class='line-number'>32</span>
+<span class='line-number'>33</span>
+<span class='line-number'>34</span>
+<span class='line-number'>35</span>
+<span class='line-number'>36</span>
+<span class='line-number'>37</span>
+<span class='line-number'>38</span>
+<span class='line-number'>39</span>
+<span class='line-number'>40</span>
+<span class='line-number'>41</span>
+<span class='line-number'>42</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
+</span><span class='line'>
+</span><span class='line'><span class="n">describe</span> <span class="nb">String</span> <span class="k">do</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">subject</span><span class="p">(</span><span class="ss">:string</span><span class="p">)</span> <span class="p">{</span> <span class="s2">&quot;abcdef ghijkl mno</span><span class="se">\n</span><span class="s2">pqr stuvwx yzabcd efghij&quot;</span> <span class="p">}</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.wrap&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;wraps long lines at word boundaries&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">8</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot;abcdef</span><span class="se">\n</span><span class="s2">ghijkl</span><span class="se">\n</span><span class="s2">mno</span><span class="se">\n</span><span class="s2">pqr</span><span class="se">\n</span><span class="s2">stuvwx</span><span class="se">\n</span><span class="s2">yzabcd</span><span class="se">\n</span><span class="s2">efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">16</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot;abcdef ghijkl</span><span class="se">\n</span><span class="s2">mno</span><span class="se">\n</span><span class="s2">pqr stuvwx</span><span class="se">\n</span><span class="s2">yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.indent&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents text with a default of 4 spaces&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents text with given number of spaces&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">2</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.indent_with_wrap&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents and wraps long text&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent_with_wrap</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd</span><span class="se">\n</span><span class="s2"> efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent_with_wrap</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+
+]]></content>
+ </entry>
+
+ <entry>
<title type="html"><![CDATA[The overdue silence]]></title>
<link href="http://nikhgupta.com/personal/diary/the-overdue-silence/"/>
<updated>2014-01-01T13:18:00+05:30</updated>
View
21 blog/archives/index.html
@@ -9,7 +9,7 @@
<meta name="author" content="Nikhil Gupta">
- <meta name="description" content="2014 Jan 1 diary, personal The overdue silence Jan 1 code wish yourself a happy new year 2013 Oct 27 code Custom Domains on Github Pages without &hellip;">
+ <meta name="description" content="2014 Jan 31 code wrapping long lines in ruby for display in source files Jan 1 diary, personal The overdue silence Jan 1 code wish yourself a happy &hellip;">
<!-- http://t.co/dKP3o1e -->
@@ -117,6 +117,25 @@
<article itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
+ <span class="date"><time datetime="2014-01-31T02:42:00+05:30" itemprop="datePublished">Jan 31</time></span>
+ <br>
+ <span class="tags">
+
+
+ <a class='category' href='/blog/categories/code/'>code</a>
+
+
+</span>
+
+ </div>
+ <h1 class="title" itemprop="name"><a href="/code/wrapping-long-lines-in-ruby-for-display-in-source-files/">wrapping long lines in ruby for display in source files</a></h1>
+</article>
+
+
+
+
+<article itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
+ <div class="meta">
<span class="date"><time datetime="2014-01-01T13:18:00+05:30" itemprop="datePublished">Jan 1</time></span>
<br>
<span class="tags">
View
358 blog/categories/code/atom.xml
@@ -4,7 +4,7 @@
<title><![CDATA[Category: code | Nikhil Gupta]]></title>
<link href="http://nikhgupta.com/blog/categories/code/atom.xml" rel="self"/>
<link href="http://nikhgupta.com/"/>
- <updated>2014-01-02T21:03:57+05:30</updated>
+ <updated>2014-01-31T03:58:52+05:30</updated>
<id>http://nikhgupta.com/</id>
<author>
<name><![CDATA[Nikhil Gupta]]></name>
@@ -14,6 +14,148 @@
<entry>
+ <title type="html"><![CDATA[wrapping long lines in ruby for display in source files]]></title>
+ <link href="http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/"/>
+ <updated>2014-01-31T02:42:00+05:30</updated>
+ <id>http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files</id>
+ <content type="html"><![CDATA[<p>While working on a gem, I needed to generate ruby classes on the fly. Yeah,
+I know this should never be the case, unless I needed around 600+ classes (I was
+converting schema.org schemas to ruby classes).</p>
+
+<p>Now, this required me to add descriptive text inside these classes, but the text
+was very long and I hated how the text overflowed the screen. I use vim, which
+promptly highlights the 80-char column marker on my source files, and I wanted
+this text to stick to that. Effectively, I wanted something like the following:</p>
+
+<p>```ruby
+module MyModule
+ class MyClass</p>
+
+<pre><code>describes "A really long text that respects the 80-char column marker and
+wraps nicely when it exceeds this length."
+</code></pre>
+
+<p> end
+end
+```</p>
+
+<!-- more -->
+
+
+<p>I knew that Rails provided a nearly similar method with the name <code>word_wrap</code>,
+and I tweaked it a bit to this:</p>
+
+<p>```ruby
+class String
+ def wrap options = {}</p>
+
+<pre><code>width = options.fetch(:width, 76)
+self.strip.split("\n").collect do |line|
+ line.length &gt; width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n") : line
+end.map(&amp;:strip).join("\n")
+</code></pre>
+
+<p> end</p>
+
+<p> def indent options = {}</p>
+
+<pre><code>spaces = " " * options.fetch(:spaces, 4)
+self.gsub(/^/, spaces).gsub(/^\s*$/, '')
+</code></pre>
+
+<p> end</p>
+
+<p> def indent_with_wrap options = {}</p>
+
+<pre><code>spaces = options.fetch(:spaces, 4)
+width = options.fetch(:width, 80)
+width = width &gt; spaces ? width - spaces : 1
+self.wrap(width: width).indent(spaces: spaces)
+</code></pre>
+
+<p> end
+end
+```</p>
+
+<p>The last method, i.e. <code>indent_with_wrap</code> is what I am interested in, really.
+Now, this might not be the best implementation for what I want, but it works and
+is self-explanatory to me.</p>
+
+<p>So, now, I can create my ERB template like this to get the desired results:</p>
+
+<p>```ruby
+module &lt;%= module_name %>
+ class &lt;%= class_name %></p>
+
+<pre><code>&lt;%= "describes #{description}".indent_with_wrap spaces: 4, width: 80 %&gt;
+</code></pre>
+
+<p> end
+end
+```</p>
+
+<h3>Specs</h3>
+
+<p>Although, not really required (and not fully implemented), some sample tests can
+be:</p>
+
+<p>```ruby
+require &lsquo;spec_helper&rsquo;</p>
+
+<p>describe String do</p>
+
+<p> subject(:string) { &ldquo;abcdef ghijkl mno\npqr stuvwx yzabcd efghij&rdquo; }</p>
+
+<p> describe &lsquo;.wrap&rsquo; do</p>
+
+<pre><code>it "wraps long lines at word boundaries" do
+ result = string.wrap width: 8
+ expected = "abcdef\nghijkl\nmno\npqr\nstuvwx\nyzabcd\nefghij"
+ expect(result).to eq(expected)
+ result = string.wrap width: 16
+ expected = "abcdef ghijkl\nmno\npqr stuvwx\nyzabcd efghij"
+ expect(result).to eq(expected)
+end
+</code></pre>
+
+<p> end</p>
+
+<p> describe &lsquo;.indent&rsquo; do</p>
+
+<pre><code>it "indents text with a default of 4 spaces" do
+ result = string.indent
+ expected = " abcdef ghijkl mno\n pqr stuvwx yzabcd efghij"
+ expect(result).to eq(expected)
+end
+
+it "indents text with given number of spaces" do
+ result = string.indent spaces: 2
+ expected = " abcdef ghijkl mno\n pqr stuvwx yzabcd efghij"
+ expect(result).to eq(expected)
+end
+</code></pre>
+
+<p> end</p>
+
+<p> describe &lsquo;.indent_with_wrap&rsquo; do</p>
+
+<pre><code>it "indents and wraps long text" do
+ result = string.indent_with_wrap spaces: 2, width: 20
+ expected = " abcdef ghijkl mno\n pqr stuvwx yzabcd\n efghij"
+ expect(result).to eq(expected)
+ result = string.indent_with_wrap spaces: 0, width: 20
+ expected = string.wrap width: 20
+ expect(result).to eq(expected)
+end
+</code></pre>
+
+<p> end
+end
+```</p>
+]]></content>
+ </entry>
+
+ <entry>
<title type="html"><![CDATA[wish yourself a happy new year]]></title>
<link href="http://nikhgupta.com/code/wish-yourself-a-happy-new-year/"/>
<updated>2014-01-01T01:19:00+05:30</updated>
@@ -282,218 +424,4 @@ installed.</li>
]]></content>
</entry>
- <entry>
- <title type="html"><![CDATA[Vim as PHP IDE: Ctags, and Code Browsing]]></title>
- <link href="http://nikhgupta.com/code/using-vim-php-ide-exuberant-ctags-code-browsing/"/>
- <updated>2012-04-01T00:00:00+05:30</updated>
- <id>http://nikhgupta.com/code/using-vim-php-ide-exuberant-ctags-code-browsing</id>
- <content type="html"><![CDATA[<p>So, past few weeks, I have been really fascinated with the all powerful Vim.
-And, I often wonder why: I took so much time switching to it? Probably, the
-steep learning curve! It has to be.</p>
-
-<p>Anyways, so I have really really progressed with Vim this time, as compared to
-my earlier trials. I am quite efficient with Vim now &ndash; even writing this blog
-post on it. I know how to move quickly, make efficient uses of search and
-replace, using Vim scripts etc. I have really really found plugins like
-Gundo, Fugitive, Tagbar, Syntastic, Snipmate, NerdTree, and Powerline to be
-useful and not to mention plugins like Vim-Rails which make my day-to-day
-tasks easier with a particular language. Oh, and did I mention the incredible
-<code>!</code> and <code>@</code> operators? They just rock!</p>
-
-<p>But, well. This post is about using Ctags with Vim to make Vim feel more like
-an IDE and readily use Source Code Browsing within frameworks, large code
-projects, etc. Here, in this post, I have specified my configuration/process
-for using Ctags with MacVim for a CakePHP based project.</p>
-
-<!-- more -->
-
-
-<h3>The Process</h3>
-
-<p>Well, I would try to create my Ctags on the fly, using Git hooks. This lets me
-simply checkout, merge or commit to a branch and my tags would be generated
-for the code in question. Moreover, these Ctags generated files are stored
-inside <code>.git</code> directory, which keeps our repositories clean (no need to add
-a file to <code>.gitignore</code>). Also, I would prefer a centralized place where I can
-manage any script I use to create these Ctags, and hence, if after about an
-year I think I need a change in the way script is working, or if say I want to
-add support for or remove support for a language, I should be able to do it
-from a single place and affect all my git repositories using these Ctags
-hooks.</p>
-
-<p>For this to work, i.e. for Vim to pick up our Ctags (that will be created in
-<code>.git/tags</code> file) we need to have <code>Fugitive</code> installed (which I will really
-really recommend anyways). This will make Vim look for Ctags in a <code>.git/tags</code>
-and <code>.git/&lt;lang&gt;.tags</code> file in our repository, irrespective of the current
-working directory.</p>
-
-<p>Let s set up a default set of hooks that Git will use as a template when
-creating or cloning a repository (requires Git 1.7.1 or newer):</p>
-
-<p><code>bash
-git config --global init.templatedir '~/.git_template'
-mkdir -p ~/.git_template/hooks
-</code></p>
-
-<p>I, usually, put all my Git related stuff in <code>~/Code/__dotfiles/git/</code> and
-hence, the above template directory lands up as
-<code>~/Code/__dotfiles/git/template/</code> with all the hooks, in my
-<a href="http://github.com/nikhgupta/dotfiles">.dotfiles</a> repository.</p>
-
-<p>Now onto the first hook, which isn&rsquo;t actually a hook at all, but rather
-a script the other hooks will call. Place in <code>~/.git_template/hooks/ctags</code> and
-mark as executable:</p>
-
-<p>``` bash</p>
-
-<h1>!/bin/sh</h1>
-
-<p>rm -f .git/tags ctags &mdash;tag-relative -Rf.git/tags \
- &mdash;exclude=.git &mdash;languages=-javascript,sql
-```</p>
-
-<p>Although, make sure that the <code>ctags</code> version is the GNU one, and the above
-script runs without showing any errors. If installing via <code>Homebrew</code> make sure
-that the <code>which ctags</code> command shows the correct <code>ctags</code> executable path.</p>
-
-<p>Making this a separate script makes it easy to invoke <code>.git/hooks/ctags</code> for
-a one-off re-index (or <code>git config --global alias.ctags '!.git/hooks/ctags'</code>,
-then <code>git ctags</code>), as well as easy to edit for that special case repository
-that needs a different set of options to ctags. For example, I might want to
-re-enable indexing for JavaScript or SQL files, which I&rsquo;ve disabled here
-because I&rsquo;ve found both to be of limited value and noisy in the warning
-department. And, in these edge cases, all I need is to modify the
-corresponding <code>.git/hooks/ctags</code> script in that git repository. Neat and
-clean.</p>
-
-<p>We still need to create the actual hooks that the Git will copy to all
-repositories that are (re)initialized using the <code>git init</code> command. All the
-files in the <code>~/.git_template/hooks</code> folder needs to be marked as executables.
-You should use the following content for: <code>post-commit</code>, <code>post-merge</code>, and
-<code>post-checkout</code> hooks (which simply calls the <code>ctags</code> hook, we create above):</p>
-
-<p>``` bash</p>
-
-<h1>!/bin/sh</h1>
-
-<p>.git/hooks/ctags >/dev/null 2>&amp;1 &amp;
-```</p>
-
-<p>Additionally, if you feel tags should also be generated on using <code>git rebase</code>,
-add the following code to <code>post-rewrite</code> hook:</p>
-
-<p>``` bash</p>
-
-<h1>!/bin/sh</h1>
-
-<p>case &ldquo;$1&rdquo; in
- rebase) exec .git/hooks/post-merge ;;
-esac
-```</p>
-
-<p>Once, the above is done, all we need to do is to issue a <code>git init</code> command to
-create Ctags for an already existing repository. All new git repositories will
-automatically include these hooks and hence, will create the Ctags file on the
-fly. Pure Automation. ;)</p>
-
-<h3>Global Projects</h3>
-
-<p>There are some projects that I always want available for source code browsing
-like the Rails source code itself. For such projects, I would generally run
-a one-off shell script that does this work for me, and store the generated
-tags file in a directory such as <code>~/.tags/</code> with a suitable name. O'course it
-does not make sense to version this directory.</p>
-
-<p>``` bash</p>
-
-<h1>!/usr/bin/env sh</h1>
-
-<p>RubyVersion=&ldquo;1.9.3-p194&rdquo;
-LaravelPath=&ldquo;${HOME}/Code/projects/lab/laravel&rdquo;</p>
-
-<p>mkdir -p &ldquo;${HOME}/.ctags&rdquo;</p>
-
-<h1>RVM: Ruby version 1.9.3-p194</h1>
-
-<p>if [ -n &ldquo;${RubyVersion}&rdquo; ]; then
- mkdir -p &ldquo;${HOME}/.ctags/ruby/${RubyVersion}&rdquo;
- TAG_FILE=&ldquo;${HOME}/.ctags/ruby/${RubyVersion}/tags&rdquo;
- rm -f &ldquo;${TAG_FILE}&rdquo;
- for rubydir in <code>find "${HOME}/.rvm/gems" -type d -depth 1 -iname "*${RubyVersion}*"</code>; do</p>
-
-<pre><code>if [ -d "${rubydir}/gems" ]; then
- echo "Looking inside: ${rubydir}/gems"
- ctags --recurse --tag-relative \
- --languages=-javascript,sql \
- --exclude=.git \
- --sort=yes --append \
- -f "${TAG_FILE}" \
- $rubydir/gems/*
-fi
-</code></pre>
-
-<p> done
- echo
- echo &ldquo;&mdash;&mdash;&mdash; generated ctags for: ${RubyVersion} &mdash;&mdash;&mdash;&rdquo;
- cat &ldquo;${TAG_FILE}&rdquo; | wc
- echo &ldquo;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;&rdquo;
- echo
-fi</p>
-
-<h1>Laravel</h1>
-
-<p>if [ -n &ldquo;${LaravelPath}&rdquo; -a -d &ldquo;${LaravelPath}&rdquo; ]; then
- mkdir -p &ldquo;${HOME}/.ctags/php/laravel&rdquo;
- TAG_FILE=&ldquo;${HOME}/.ctags/php/laravel/tags&rdquo;
- rm -f &ldquo;${TAG_FILE}&rdquo;
- echo &ldquo;Looking inside: ${LaravelPath}&rdquo;
- ctags &mdash;recurse &mdash;tag-relative \</p>
-
-<pre><code>--languages=-javascript,sql \
---exclude=.git \
---sort=yes --append \
---PHP-kinds=+cf \
---regex-PHP='/abstract class ([^ ]*)/\1/c/' \
---regex-PHP='/interface ([^ ]*)/\1/c/' \
---regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/' \
--f "${TAG_FILE}" \
-$LaravelPath
-</code></pre>
-
-<p> echo
- echo &ldquo;&mdash;&mdash;&mdash;&mdash; generated ctags for: Laravel &mdash;&mdash;&mdash;&ndash;&rdquo;
- cat &ldquo;${TAG_FILE}&rdquo; | wc
- echo &ldquo;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;&rdquo;
- echo
-fi
-```</p>
-
-<p>Finally, I set the following inside my <code>~/.vimrc</code> file:</p>
-
-<p><code>vim
-set tags=./tags;/
-augroup TagFileType
- autocmd!
- autocmd FileType * setl tags&lt;
- autocmd FileType * exe 'setl tags+=~/.ctags/' . &amp;filetype . '/*/tags'
-augroup END
-</code></p>
-
-<p>This basically tells vim to only load ctags for the current filetype, i.e. for
-a <code>ruby</code> buffer, vim will load all ctags files inside <code>~/.ctags/ruby</code>
-directory, and no ctags file inside <code>~/.ctags/php</code></p>
-
-<h3>Epilogue</h3>
-
-<p>(Now, that I have completed writing the post and I read it again, it seems
-that this post feels like a rewrite <a href="http://tbaggery.com/2011/08/08/effortless-ctags-with-git.html">a post by tPope</a>. Well,
-I was originally thinking of a different way to create these Ctags, but as
-I went deeper and deeper, I realized that what
-<a href="https://github.com/tpope">tPope</a> describes is so natural and easy, that
-I have to ditch whatever it is &ndash; what I was doing now. And, do not forget,
-I am less than a month old with Vim and I am talking Ctags with Vim. I am
-allowed to reherse my steps :P )</p>
-]]></content>
- </entry>
-
</feed>
View
19 blog/categories/code/index.html
@@ -116,6 +116,25 @@
<article itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
+ <span class="date"><time datetime="2014-01-31T02:42:00+05:30" itemprop="datePublished">Jan 31</time></span>
+ <br>
+ <span class="tags">
+
+
+ <a class='category' href='/blog/categories/code/'>code</a>
+
+
+</span>
+
+ </div>
+ <h1 class="title" itemprop="name"><a href="/code/wrapping-long-lines-in-ruby-for-display-in-source-files/">wrapping long lines in ruby for display in source files</a></h1>
+</article>
+
+
+
+
+<article itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
+ <div class="meta">
<span class="date"><time datetime="2014-01-01T01:19:00+05:30" itemprop="datePublished">Jan 1</time></span>
<br>
<span class="tags">
View
2  blog/categories/diary/atom.xml
@@ -4,7 +4,7 @@
<title><![CDATA[Category: diary | Nikhil Gupta]]></title>
<link href="http://nikhgupta.com/blog/categories/diary/atom.xml" rel="self"/>
<link href="http://nikhgupta.com/"/>
- <updated>2014-01-02T21:03:57+05:30</updated>
+ <updated>2014-01-31T03:58:52+05:30</updated>
<id>http://nikhgupta.com/</id>
<author>
<name><![CDATA[Nikhil Gupta]]></name>
View
4 blog/categories/index.html
@@ -9,7 +9,7 @@
<meta name="author" content="Nikhil Gupta">
- <meta name="description" content=" Code [ 16 ] Personal [ 3 ] Diary [ 1 ] ">
+ <meta name="description" content=" Code [ 17 ] Personal [ 3 ] Diary [ 1 ] ">
<!-- http://t.co/dKP3o1e -->
@@ -112,7 +112,7 @@ <h1 class="title">Categories</h1>
<div class="entry-content"><ul>
- <li><a href="/blog/categories/code/">Code</a> [ 16 ]</li>
+ <li><a href="/blog/categories/code/">Code</a> [ 17 ]</li>
<li><a href="/blog/categories/personal/">Personal</a> [ 3 ]</li>
View
2  blog/categories/personal/atom.xml
@@ -4,7 +4,7 @@
<title><![CDATA[Category: personal | Nikhil Gupta]]></title>
<link href="http://nikhgupta.com/blog/categories/personal/atom.xml" rel="self"/>
<link href="http://nikhgupta.com/"/>
- <updated>2014-01-02T21:03:57+05:30</updated>
+ <updated>2014-01-31T03:58:52+05:30</updated>
<id>http://nikhgupta.com/</id>
<author>
<name><![CDATA[Nikhil Gupta]]></name>
View
41 blog/page/2/index.html
@@ -124,6 +124,47 @@
+<time datetime="2012-03-28T00:00:00+05:30" data-updated="true" itemprop="datePublished">Mar 28<span>th</span>, 2012</time></div>
+ <div class="tags">
+
+
+ <a class='category' href='/blog/categories/code/'>code</a>
+
+
+</div>
+
+ </div>
+ <h1 class="title" itemprop="name"><a href="/code/5minutes-to-beta-video/" itemprop="url">5minutes. To Beta Video</a></h1>
+ <div class="entry-content" itemprop="articleBody">
+ <p>Recently, I have been involved with the <a href="http://5minutes.to/">5minutes.to</a>
+application. This application helps you get up and ready with your next website
+in 5 minutes or less including email addresses, templates and what not.</p>
+
+<p>In this post, I have posted an introductory video we made for
+<a href="http://beta.5minutes.to/">5minutes.to</a>, and by all means we love it :)</p>
+
+
+
+ <a href="/code/5minutes-to-beta-video/" class="more-link">Read on &rarr;</a>
+ </div>
+
+</article>
+
+
+ <article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
+ <div class="meta">
+ <div class="date">
+
+
+
+
+
+
+
+
+
+
+
<time datetime="2012-03-20T00:00:00+05:30" data-updated="true" itemprop="datePublished">Mar 20<span>th</span>, 2012</time></div>
<div class="tags">
View
386 code/wrapping-long-lines-in-ruby-for-display-in-source-files/index.html
@@ -0,0 +1,386 @@
+
+<!DOCTYPE HTML>
+
+<html>
+
+<head>
+ <meta charset="utf-8">
+ <title>wrapping long lines in ruby for display in source files - Nikhil Gupta</title>
+ <meta name="author" content="Nikhil Gupta">
+
+
+ <meta name="description" content="Wrapping Long Lines in Ruby for Display in Source Files While working on a gem, I needed to generate ruby classes on the fly. Yeah,
+I know this &hellip;">
+
+
+ <!-- http://t.co/dKP3o1e -->
+ <meta name="HandheldFriendly" content="True">
+ <meta name="MobileOptimized" content="320">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+
+ <link href="/atom.xml" rel="alternate" title="Nikhil Gupta" type="application/atom+xml">
+
+ <link rel="canonical" href="http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/">
+ <link href="/favicon.png" rel="shortcut icon">
+ <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
+ <link href="/stylesheets/font-awesome.min.css" media="screen, projection" rel="stylesheet" type="text/css">
+ <!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
+ <link href='http://fonts.googleapis.com/css?family=Alegreya:400,700|Mouse+Memoirs' rel='stylesheet' type='text/css'>
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
+<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
+<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
+
+
+ <script type="text/javascript">
+ var _gaq = _gaq || [];
+ _gaq.push(['_setAccount', 'UA-21306915-1']);
+ _gaq.push(['_trackPageview']);
+
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+ })();
+ </script>
+
+
+</head>
+
+
+<body>
+ <div class="container">
+ <div class="left-col">
+ <div class="intrude-less">
+ <header id="header" class="inner"><div class="profilepic"> </div>
+<hgroup>
+ <h1><a href="/">Nikhil Gupta</a></h1>
+
+ <h2>God is divine. And, so is code.</h2>
+
+</hgroup>
+
+<!-- <div id="awesome"></div> -->
+
+<nav id="main-nav"><ul class="main-navigation">
+ <li><a href="/">Blog</a></li>
+ <li><a href="/blog/archives">Archives</a></li>
+</ul>
+
+</nav>
+<nav id="sub-nav">
+ <div class="social">
+
+ <a class="email" href="mailto:me@nikhgupta.com" title="Email">Email</a>
+
+
+
+
+ <a class="twitter" href="http://twitter.com/nikhgupta" title="Twitter">Twitter</a>
+
+
+ <a class="github" href="https://github.com/nikhgupta" title="GitHub">GitHub</a>
+
+
+
+
+
+
+
+
+
+ <a class="rss" href="/atom.xml" title="RSS">RSS</a>
+
+ </div>
+</nav>
+
+<div id="authorinfo">
+ <h1>An entrepreneur, developer, photographer and a perfectionist.</h1>
+ <br/>
+ <h3>&quot;I am from Jaipur, India. I am a part of the Springboards Programme, 2012, whence I love to code little devils in Ruby.&quot;</h3>
+</div>
+</header>
+ </div>
+ </div>
+ <div class="mid-col">
+
+
+
+ <div class="mid-col-container">
+ <div id="content" class="inner"><article class="post" itemscope itemtype="http://schema.org/BlogPosting">
+
+ <h1 class="title" itemprop="name">Wrapping Long Lines in Ruby for Display in Source Files</h1>
+ <div class="entry-content" itemprop="articleBody"><p>While working on a gem, I needed to generate ruby classes on the fly. Yeah,
+I know this should never be the case, unless I needed around 600+ classes (I was
+converting schema.org schemas to ruby classes).</p>
+
+<p>Now, this required me to add descriptive text inside these classes, but the text
+was very long and I hated how the text overflowed the screen. I use vim, which
+promptly highlights the 80-char column marker on my source files, and I wanted
+this text to stick to that. Effectively, I wanted something like the following:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">MyModule</span>
+</span><span class='line'> <span class="k">class</span> <span class="nc">MyClass</span>
+</span><span class='line'> <span class="n">describes</span> <span class="s2">&quot;A really long text that respects the 80-char column marker and</span>
+</span><span class='line'><span class="s2"> wraps nicely when it exceeds this length.&quot;</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+
+
+<!-- more -->
+
+
+<p>I knew that Rails provided a nearly similar method with the name <code>word_wrap</code>,
+and I tweaked it a bit to this:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">String</span>
+</span><span class='line'> <span class="k">def</span> <span class="nf">wrap</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:width</span><span class="p">,</span> <span class="mi">76</span><span class="p">)</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">strip</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">collect</span> <span class="k">do</span> <span class="o">|</span><span class="n">line</span><span class="o">|</span>
+</span><span class='line'> <span class="n">line</span><span class="o">.</span><span class="n">length</span> <span class="o">&gt;</span> <span class="n">width</span> <span class="p">?</span> <span class="n">line</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/(.{1,</span><span class="si">#{</span><span class="n">width</span><span class="si">}</span><span class="sr">})(\s+|$)/</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">1</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span> <span class="p">:</span> <span class="n">line</span>
+</span><span class='line'> <span class="k">end</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:strip</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="k">def</span> <span class="nf">indent</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">spaces</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:spaces</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/^/</span><span class="p">,</span> <span class="n">spaces</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/^\s*$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="k">def</span> <span class="nf">indent_with_wrap</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
+</span><span class='line'> <span class="n">spaces</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:spaces</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="ss">:width</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
+</span><span class='line'> <span class="n">width</span> <span class="o">=</span> <span class="n">width</span> <span class="o">&gt;</span> <span class="n">spaces</span> <span class="p">?</span> <span class="n">width</span> <span class="o">-</span> <span class="n">spaces</span> <span class="p">:</span> <span class="mi">1</span>
+</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">wrap</span><span class="p">(</span><span class="ss">width</span><span class="p">:</span> <span class="n">width</span><span class="p">)</span><span class="o">.</span><span class="n">indent</span><span class="p">(</span><span class="ss">spaces</span><span class="p">:</span> <span class="n">spaces</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+<p>The last method, i.e. <code>indent_with_wrap</code> is what I am interested in, really.
+Now, this might not be the best implementation for what I want, but it works and
+is self-explanatory to me.</p>
+
+<p>So, now, I can create my ERB template like this to get the desired results:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">module</span> <span class="o">&lt;</span><span class="sx">%= module_name %&gt;</span>
+</span><span class='line'><span class="sx"> class &lt;%=</span> <span class="n">class_name</span> <span class="sx">%&gt;</span>
+</span><span class='line'><span class="sx"> &lt;%= &quot;describes </span><span class="si">#{</span><span class="n">description</span><span class="si">}</span><span class="sx">&quot;.indent_with_wrap spaces: 4, width: 80 %&gt;</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+<h3>Specs</h3>
+
+<p>Although, not really required (and not fully implemented), some sample tests can
+be:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+<span class='line-number'>7</span>
+<span class='line-number'>8</span>
+<span class='line-number'>9</span>
+<span class='line-number'>10</span>
+<span class='line-number'>11</span>
+<span class='line-number'>12</span>
+<span class='line-number'>13</span>
+<span class='line-number'>14</span>
+<span class='line-number'>15</span>
+<span class='line-number'>16</span>
+<span class='line-number'>17</span>
+<span class='line-number'>18</span>
+<span class='line-number'>19</span>
+<span class='line-number'>20</span>
+<span class='line-number'>21</span>
+<span class='line-number'>22</span>
+<span class='line-number'>23</span>
+<span class='line-number'>24</span>
+<span class='line-number'>25</span>
+<span class='line-number'>26</span>
+<span class='line-number'>27</span>
+<span class='line-number'>28</span>
+<span class='line-number'>29</span>
+<span class='line-number'>30</span>
+<span class='line-number'>31</span>
+<span class='line-number'>32</span>
+<span class='line-number'>33</span>
+<span class='line-number'>34</span>
+<span class='line-number'>35</span>
+<span class='line-number'>36</span>
+<span class='line-number'>37</span>
+<span class='line-number'>38</span>
+<span class='line-number'>39</span>
+<span class='line-number'>40</span>
+<span class='line-number'>41</span>
+<span class='line-number'>42</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
+</span><span class='line'>
+</span><span class='line'><span class="n">describe</span> <span class="nb">String</span> <span class="k">do</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">subject</span><span class="p">(</span><span class="ss">:string</span><span class="p">)</span> <span class="p">{</span> <span class="s2">&quot;abcdef ghijkl mno</span><span class="se">\n</span><span class="s2">pqr stuvwx yzabcd efghij&quot;</span> <span class="p">}</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.wrap&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;wraps long lines at word boundaries&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">8</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot;abcdef</span><span class="se">\n</span><span class="s2">ghijkl</span><span class="se">\n</span><span class="s2">mno</span><span class="se">\n</span><span class="s2">pqr</span><span class="se">\n</span><span class="s2">stuvwx</span><span class="se">\n</span><span class="s2">yzabcd</span><span class="se">\n</span><span class="s2">efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">16</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot;abcdef ghijkl</span><span class="se">\n</span><span class="s2">mno</span><span class="se">\n</span><span class="s2">pqr stuvwx</span><span class="se">\n</span><span class="s2">yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.indent&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents text with a default of 4 spaces&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents text with given number of spaces&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">2</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'>
+</span><span class='line'> <span class="n">describe</span> <span class="s1">&#39;.indent_with_wrap&#39;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">it</span> <span class="s2">&quot;indents and wraps long text&quot;</span> <span class="k">do</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent_with_wrap</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="s2">&quot; abcdef ghijkl mno</span><span class="se">\n</span><span class="s2"> pqr stuvwx yzabcd</span><span class="se">\n</span><span class="s2"> efghij&quot;</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="n">result</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">indent_with_wrap</span> <span class="ss">spaces</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expected</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">wrap</span> <span class="ss">width</span><span class="p">:</span> <span class="mi">20</span>
+</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">result</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">expected</span><span class="p">)</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+
+</div>
+
+
+ <div class="author" style="text-align:right">- by Nikhil Gupta</div>
+</article>
+
+ <div class="share">
+ <div class="addthis_toolbox addthis_default_style ">
+
+ <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
+
+
+ <a class="addthis_button_tweet"></a>
+
+
+ <a class="addthis_counter addthis_pill_style"></a>
+ </div>
+ <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid="></script>
+</div>
+
+
+
+<section id="comment">
+ <h1 class="title">Comments</h1>
+ <div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
+</div>
+</section>
+
+</div>
+ </div>
+ <footer id="footer" class="inner"><p>
+ Copyright &copy; 2014 - Nikhil Gupta -
+ <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
+</p>
+
+</footer>
+
+
+<script type="text/javascript">
+ var disqus_shortname = 'nikhgupta';
+
+
+ // var disqus_developer = 1;
+ var disqus_identifier = 'http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/';
+ var disqus_url = 'http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/';
+ var disqus_script = 'embed.js';
+
+ (function () {
+ var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
+ dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
+ }());
+</script>
+
+
+
+<div id="fb-root"></div>
+<script>(function(d, s, id) {
+ var js, fjs = d.getElementsByTagName(s)[0];
+ if (d.getElementById(id)) {return;}
+ js = d.createElement(s); js.id = id; js.async = true;
+ js.src = "//connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1";
+ fjs.parentNode.insertBefore(js, fjs);
+}(document, 'script', 'facebook-jssdk'));</script>
+
+
+
+
+
+ <script type="text/javascript">
+ (function(){
+ var twitterWidgets = document.createElement('script');
+ twitterWidgets.type = 'text/javascript';
+ twitterWidgets.async = true;
+ twitterWidgets.src = '//platform.twitter.com/widgets.js';
+ document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
+ })();
+ </script>
+
+
+
+
+
+ </div>
+ </div>
+</body>
+</html>
View
101 index.html
@@ -124,6 +124,66 @@
+<time datetime="2014-01-31T02:42:00+05:30" data-updated="true" itemprop="datePublished">Jan 31<span>st</span>, 2014</time></div>
+ <div class="tags">
+
+
+ <a class='category' href='/blog/categories/code/'>code</a>
+
+
+</div>
+
+ </div>
+ <h1 class="title" itemprop="name"><a href="/code/wrapping-long-lines-in-ruby-for-display-in-source-files/" itemprop="url">Wrapping Long Lines in Ruby for Display in Source Files</a></h1>
+ <div class="entry-content" itemprop="articleBody">
+ <p>While working on a gem, I needed to generate ruby classes on the fly. Yeah,
+I know this should never be the case, unless I needed around 600+ classes (I was
+converting schema.org schemas to ruby classes).</p>
+
+<p>Now, this required me to add descriptive text inside these classes, but the text
+was very long and I hated how the text overflowed the screen. I use vim, which
+promptly highlights the 80-char column marker on my source files, and I wanted
+this text to stick to that. Effectively, I wanted something like the following:</p>
+
+<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
+<span class='line-number'>2</span>
+<span class='line-number'>3</span>
+<span class='line-number'>4</span>
+<span class='line-number'>5</span>
+<span class='line-number'>6</span>
+</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">MyModule</span>
+</span><span class='line'> <span class="k">class</span> <span class="nc">MyClass</span>
+</span><span class='line'> <span class="n">describes</span> <span class="s2">&quot;A really long text that respects the 80-char column marker and</span>
+</span><span class='line'><span class="s2"> wraps nicely when it exceeds this length.&quot;</span>
+</span><span class='line'> <span class="k">end</span>
+</span><span class='line'><span class="k">end</span>
+</span></code></pre></td></tr></table></div></figure>
+
+
+
+
+
+
+ <a href="/code/wrapping-long-lines-in-ruby-for-display-in-source-files/" class="more-link">Read on &rarr;</a>
+ </div>
+
+</article>
+
+
+ <article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
+ <div class="meta">
+ <div class="date">
+
+
+
+
+
+
+
+
+
+
+
<time datetime="2014-01-01T13:18:00+05:30" data-updated="true" itemprop="datePublished">Jan 1<span>st</span>, 2014</time></div>
<div class="tags">
@@ -508,47 +568,6 @@ <h1 class="title" itemprop="name"><a href="/code/vim-awesomeness-gundo-and-tabul
</article>
-
- <article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
- <div class="meta">
- <div class="date">
-
-
-
-
-
-
-
-
-
-
-
-<time datetime="2012-03-28T00:00:00+05:30" data-updated="true" itemprop="datePublished">Mar 28<span>th</span>, 2012</time></div>
- <div class="tags">
-
-
- <a class='category' href='/blog/categories/code/'>code</a>
-
-
-</div>
-
- </div>
- <h1 class="title" itemprop="name"><a href="/code/5minutes-to-beta-video/" itemprop="url">5minutes. To Beta Video</a></h1>
- <div class="entry-content" itemprop="articleBody">
- <p>Recently, I have been involved with the <a href="http://5minutes.to/">5minutes.to</a>
-application. This application helps you get up and ready with your next website
-in 5 minutes or less including email addresses, templates and what not.</p>
-
-<p>In this post, I have posted an introductory video we made for
-<a href="http://beta.5minutes.to/">5minutes.to</a>, and by all means we love it :)</p>
-
-
-
- <a href="/code/5minutes-to-beta-video/" class="more-link">Read on &rarr;</a>
- </div>
-
-</article>
-
</div>
<nav id="pagenavi">
View
12 sitemap.xml
@@ -74,15 +74,21 @@
</url>
<url>
<loc>http://nikhgupta.com/code/wish-yourself-a-happy-new-year/</loc>
- <lastmod>2014-01-02T21:03:52+05:30</lastmod>
+ <lastmod>2014-01-02T21:22:56+05:30</lastmod>
</url>
<url>
<loc>http://nikhgupta.com/personal/diary/the-overdue-silence/</loc>
<lastmod>2014-01-02T20:01:24+05:30</lastmod>
</url>
<url>
+ <loc>
+ http://nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/
+ </loc>
+ <lastmod>2014-01-31T03:57:38+05:30</lastmod>
+ </url>
+ <url>
<loc>http://nikhgupta.com/blog/archives/</loc>
- <lastmod>2014-01-02T21:03:52+05:30</lastmod>
+ <lastmod>2014-01-31T03:57:38+05:30</lastmod>
</url>
<url>
<loc>http://nikhgupta.com/blog/categories/</loc>
@@ -90,7 +96,7 @@
</url>
<url>
<loc>http://nikhgupta.com/</loc>
- <lastmod>2014-01-02T21:03:52+05:30</lastmod>
+ <lastmod>2014-01-31T03:57:38+05:30</lastmod>
</url>
<url>
<loc>http://nikhgupta.com/robots.txt</loc>
Please sign in to comment.
Something went wrong with that request. Please try again.