tag:blogger.com,1999:blog-21863408352155500322024-03-08T16:38:15.749+05:30Ruby on RailsPersonal page open for public for sharing my ventures, obstacles and experience in case it helpz anyone(and ofcourse myself in future...)
I will be obliged if you rectify me wherever anyone think I goes wrong.
-a developing RoR developer...Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-2186340835215550032.post-86613512566231034362014-03-27T16:06:00.002+05:302014-03-28T13:00:30.564+05:30Ruby Gotchas<div dir="ltr" style="text-align: left;" trbidi="on">
<a class="lightbox" href="http://blog.elpassion.com/wp-content/uploads/2014/02/ruby-gotchas.png" rel="bookmark" target="_blank" title="Ruby Gotchas that will come back to haunt you"> </a>
<br />
Most Ruby on Rails beginners get excited by the framework and
start crafting applications without any knowledge of the language. And
there’s nothing wrong about it. At least, unless those beginners persist
in such approach and become <i>senior</i> developers without any knowledge of the language.<br />
<span id="more-580"></span><br />
Anyway, sooner or later, beginners or experienced programmers, we all
run into so-called Ruby Gotchas – those small subtleties that
hide from our sight for hours of hardcore debugging (<i>logger.debug "*** #{object} ****"</i>) and then we go <i>Ohh..That was that??! Really, this time I WILL read that book with a hammer on the cover!</i> Or, rather, we go <i>shit!</i> and fall asleep.<br />
<br />
Here is a list of popular Ruby gotchas and curiosities that
developers should be aware of. For each case, there’s an example of
confusing and/or error-prone code.<br />
<br />
<br />
<hr />
<h2>
<code><br /></code></h2>
<h2>
<code>and / or</code> is NOT the same as <code>&& / ||</code></h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqk"><span class="unknown"> surprise = </span><span class="kw1">true</span><span class="unknown"> </span><span class="kw1">and</span><span class="unknown"> </span><span class="kw1">false</span><span class="unknown"> </span><span class="co1"># => surprise is true</span><span class="unknown">
surprise = </span><span class="kw1">true</span><span class="unknown"> && </span><span class="kw1">false</span><span class="unknown"> </span><span class="co1"># => surprise is false</span></pre>
<br />
<b>Good practice</b><br />
Use only <code>&& / ||</code> operators.<br />
<b><br /></b>
<b>In detail</b><br />
<ul>
<li><code>and / or</code> operators have lower <a href="http://www.techotopia.com/index.php/Ruby_Operator_Precedence#Operator_Precedence_Table" target="_blank" title="precedence">precedence</a> than <code>&& / ||</code></li>
<li><code>and / or</code> have lower precedence than <code>=</code> assignment operator, while <code>&& / ||</code> are of higher precedence</li>
<li><code>and</code> and <code>or</code> have the same precedence, while <code>&&</code> has higher precedence than <code>||</code></li>
</ul>
The first example becomes clearer when we add parentheses that illustrate how using `<code>and`</code> differs from `<code>&&`</code>:<br />
<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlql"><span class="br0">(</span><span class="unknown">surprise = </span><span class="kw1">true</span><span class="br0">)</span><span class="unknown"> </span><span class="kw1">and</span><span class="unknown"> </span><span class="kw1">false</span><span class="unknown"> </span><span class="co1"># => surprise is true</span><span class="unknown">
surprise = </span><span class="br0">(</span><span class="kw1">true</span><span class="unknown"> && </span><span class="kw1">false</span><span class="br0">)</span><span class="unknown"> </span><span class="co1"># => surprise is false</span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlql"><span class="co1">
</span></pre>
Some say: use <code>and / or</code> for flow control and <code>&& / ||</code> for boolean operations. I will say: don’t use keyword versions (<code>and / or / not</code>) at all (and go with more verbose <code>if</code>s and <code>unless</code>es). Less ambiguity, less confusion, less bugs.<br />
<small>More: <a href="http://stackoverflow.com/questions/2083112/difference-between-or-and-in-ruby" target="_blank" title="Difference between “or” and || in Ruby?">Difference between “or” and || in Ruby?</a></small><br />
<br />
<hr />
<h2>
<code>eql?</code> is NOT the same as <code>==</code> (and NOT the same as <code>equal?</code> or <code>===</code>)</h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><span class="nu0">1</span><span class="unknown"> == </span><span class="nu0">1.0</span><span class="unknown"> </span><span class="co1"># => true</span><span class="unknown">
</span><span class="nu0">1</span><span class="unknown">.</span><span class="me0">eql</span><span class="unknown">? </span><span class="nu0">1.0</span><span class="unknown"> </span><span class="co1"># => false</span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><span class="co1">
</span></pre>
<b>Good practice</b><br />
Use only <code>==</code> operator.<br />
<b><br /></b>
<b>In detail</b><br />
<b><br /></b>
<code> ==</code>, <code>===</code>, <code>eql?</code> and <code>equal?</code> are all different operators, meant for different usage in different situations. You should always use <code>==</code> operator for comparing things, unless you have some specific needs (like you <i>really</i> need to differ <code>1.0</code> from <code>1</code>) or manually override one of the equality operators for whatever reason.<br />
<br />
Yes, the <code>eql?</code> version may look <i>smarter</i> than plain old <code>==</code> comparison, but does it really do what you meant it to do, like, just <i>compare some things</i>?<br />
<small>More: <a href="http://stackoverflow.com/questions/7156955/whats-the-difference-between-equal-eql-and" target="_blank" title="What’s the difference between equal?, eql?, ===, and ==?">What’s the difference between equal?, eql?, ===, and ==?</a></small><br />
<br />
<br class="Apple-interchange-newline" />
<hr />
<h2>
<code>a ||= b is not a = a || b</code></h2>
<div>
<br /></div>
<div>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><span class="nu0">A common misconception is that <i>a </i>||= <i>b </i>is equivalent to<i> a = a</i> || <i>b, </i>but it behaves<i> </i>like<i> a </i>|| <i>a = b</i></span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><span class="nu0"><i>
</i></span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm">In <i>a = a </i>|| <i>b, `</i>a` is set to something by the statement on every run, whereas with <i>a = </i>|| <i>a = b,</i> `a` is only set if `a` is logically false (i.e. if it's <i>nil </i>or <i>false</i>) because || is 'short circuiting'. That is, if the left hand side of the || comparison is true, there's no need to check the right hand side.</pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm">An example goes here: </pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><pre style="background-color: #f6ffff; border-color: rgb(170, 221, 221); border-style: dashed dashed dashed solid; border-width: 1px 1px 1px 3px; color: #111111; font-family: consolas, menlo, monaco, 'Liberation Mono', 'Droid Sans Mono', courier, monospace; font-size: 15px; line-height: 20px; overflow: auto; padding: 24px;">h = {}
def h.[]=(k, v)
puts "Setting hash key #{k} with #{v.inspect}"
super
end
# 1. The standard ||= approach
h[:x] ||= 10
h[:x] ||= 20
# 2. The a = a || b approach
h[:y] = h[:y] || 10
h[:y] = h[:y] || 20
# 3. The a || a = b approach
h[:z] || h[:z] = 10
h[:z] || h[:z] = 20</pre>
</pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm">Output:</pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"><pre style="background-color: #f6ffff; border-color: rgb(170, 221, 221); border-style: dashed dashed dashed solid; border-width: 1px 1px 1px 3px; color: #111111; font-family: consolas, menlo, monaco, 'Liberation Mono', 'Droid Sans Mono', courier, monospace; font-size: 15px; line-height: 20px; overflow: auto; padding: 24px;">Setting hash key x with 10
Setting hash key y with 10
Setting hash key y with 10
Setting hash key z with 10</pre>
</pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"> </pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm">here <i>y </i>is unnecessarily set twice because of the general approach we consider.</pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm"></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqm">Note : <span style="background-color: #ffffaa; color: #444400; font-family: consolas, menlo, monaco, 'Liberation Mono', 'Droid Sans Mono', courier, monospace; font-size: 15px; line-height: 21.279998779296875px; word-spacing: 2.2799999713897705px;">Do not confuse [op]= with anything related to ||= or &&=. They're entirely different ideas and are implemented entirely different[ly].</span></pre>
</div>
<div>
<code><br /></code></div>
<hr />
<h2>
<code>super</code> is NOT the same as <code>super()</code></h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqn"><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> show
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="st0">'Foo#show'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Bar</span><span class="unknown"> < </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> show</span><span class="br0">(</span><span class="unknown">text</span><span class="br0">)</span><span class="unknown">
</span><span class="kw1">super</span><span class="unknown">
</span><span class="kw2">puts</span><span class="unknown"> text
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw3">Bar</span><span class="unknown">.</span><span class="me0">new</span><span class="unknown">.</span><span class="me0">show</span><span class="br0">(</span><span class="st0">'test'</span><span class="br0">)</span><span class="unknown"> </span><span class="co1"># ArgumentError: wrong number of arguments (1 for 0)</span></pre>
<b><br /></b>
<b>Good practice</b><br />
This is one of the places where omitting the parentheses is not only a
matter of taste (or conventions), but actually changes the program
logic.<br />
<b><br /></b>
<b>In detail</b><br />
<ul>
<li><code>super</code> (without parentheses) will call parent method <b>with exactly the same arguments that were passed to the original method</b> (so <code>super</code> inside <code>Bar#show</code> becomes <code>super('test')</code> here, causing an error, because parent method does not take any arguments).</li>
<li><code>super()</code> (with parentheses) will call parent method <b>without any arguments</b>, just as expected.</li>
</ul>
<small>More: <a href="http://stackoverflow.com/questions/4632224/super-keyword-in-ruby" target="_blank" title="Super keyword in Ruby">Super keyword in Ruby</a></small><br />
<br />
<hr />
<h2>
Your exception must not be an <code>Exception</code></h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqo"><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">MyException</span><span class="unknown"> < </span><span class="kw2">Exception</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">begin</span><span class="unknown">
</span><span class="kw2">raise</span><span class="unknown"> </span><span class="kw3">MyException</span><span class="unknown">
</span><span class="kw1">rescue</span><span class="unknown">
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="st0">'Caught it!'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="co1"># MyException: MyException</span><span class="unknown">
</span><span class="co1"># from (irb):17</span><span class="unknown">
</span><span class="co1"># from /Users/amit/.rbenv/versions/2.1.0/bin/irb:11:in `<main>'</span></pre>
<small>(This code will not catch <code>MyException</code> and the message <code>'Caught it!'</code> will not be displayed.)</small><br />
<small><br /></small>
<b>Good practice</b><br />
<ul>
<li>When defining your own exception class, inherit from <code>StandardError</code> or any of its descendants (the more specific, the better). Never use <code>Exception</code> for the parent.</li>
<li>Never <code>rescue Exception</code>. If you want to do some general rescue, leave <code>rescue</code> statement empty (or use <code>rescue => e</code> to access the error).</li>
</ul>
<b>In detail</b><br />
<ul>
<li>When you leave <code>rescue</code> statement empty, it means it will catch exceptions that inherit from <code>StandardError</code>, not <code>Exception</code>.</li>
<li>When you <code>rescue Exception</code> (which you should not),
you’ll catch errors you won’t be able to recover from (like out of
memory error). Also, you’ll catch system signals like SIGTERM, and in
effect you won’t be able to terminate your script using CTRL-C.</li>
</ul>
<small>More: <a href="http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby" target="_blank" title="Why is it bad style to `rescue Exception => e` in Ruby?">Why is it bad style to `rescue Exception => e` in Ruby?</a></small><br />
<br />
<hr />
<h2>
<code>class Foo::Bar</code> is NOT the same as <code>module Foo; class Bar</code></h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqp"><span class="kw3">MY_SCOPE</span><span class="unknown"> = </span><span class="st0">'Global'</span><span class="unknown">
</span><span class="kw1">module</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw3">MY_SCOPE</span><span class="unknown"> = </span><span class="st0">'Foo Module'</span><span class="unknown">
</span><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Bar</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> scope1
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="kw3">MY_SCOPE</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">::</span><span class="kw3">Bar</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> scope2
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="kw3">MY_SCOPE</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">::</span><span class="kw3">Bar</span><span class="unknown">.</span><span class="me0">new</span><span class="unknown">.</span><span class="me0">scope1</span><span class="unknown"> </span><span class="co1"># => "Foo Module"</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">::</span><span class="kw3">Bar</span><span class="unknown">.</span><span class="me0">new</span><span class="unknown">.</span><span class="me0">scope2</span><span class="unknown"> </span><span class="co1"># => "Global"</span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqp"><span class="co1">
</span></pre>
<b>Good practice</b><br />
Always use longer, more verbose version with classes wrapped by modules:<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqq"><span class="kw1"> module</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Bar</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1"> end</span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqq"><span class="kw1">
</span></pre>
<b>In detail</b><br />
<ul>
<li><code>module</code> keyword (as well as <code>class</code> and <code>def</code>) will create new lexical scope for all the things you put inside. So, our <code>module Foo</code> creates the scope <code>'Foo'</code> in which our <code>MY_SCOPE</code> constant with <code>'Foo Module'</code> value resides.</li>
<li>Inside this module, we declare <code>class Bar</code>, which creates new lexical scope (named <code>'Foo::Bar'</code>), which has access to its parent scope (<code>'Foo'</code>) and all constants declared in it.</li>
<li>However, when you declare Foo::Bar with this <code>::</code> “shortcut”: <code>class Foo::Bar</code>, it creates another lexical scope, which is also named <code>'Foo::Bar'</code>, but here, it has no parent, and thus, no access to things from <code>'Foo'</code> scope.</li>
<li>Therefore, inside <code>class Foo::Bar</code>, we have only access to <code>MY_SCOPE</code> constant declared at the beginning of the script (without any module) with value <code>'Global'</code>.</li>
</ul>
<small>More: <a href="http://stackoverflow.com/questions/15119724/ruby-lexical-scope-vs-inheritance" target="_blank" title="Ruby – Lexical scope vs Inheritance">Ruby – Lexical scope vs Inheritance</a></small><br />
<br />
<hr />
<h2>
Most <code>bang!</code> methods return <code>nil</code> when they do nothing</h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqr"><span class="st0">'foo'</span><span class="unknown">.</span><span class="me0">upcase</span><span class="unknown">! </span><span class="co1"># => "FOO"</span><span class="unknown">
</span><span class="st0">'FOO'</span><span class="unknown">.</span><span class="me0">upcase</span><span class="unknown">! </span><span class="co1"># => nil</span></pre>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqr"><span class="co1">
</span></pre>
<b>Good practice</b><br />
Never depend on built-in <code>bang!</code> methods return value, e.g. in conditional statements or in control flow:<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqs"><span class="kw3"> @name</span><span class="unknown">.</span><span class="me0">upcase</span><span class="unknown">! </span><span class="kw1">and</span><span class="unknown"> render </span><span class="kw4">:show</span></pre>
Above code can cause some unpredictable behaviour (or, to be more specific, very predictable failure when <code>@name</code> is already in uppercase). Also, it is another example why you should not use <code>and / or</code> for control-flow shortcuts. No trees will be cut if you add those two enters there:<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqt"><span class="kw3"> @name</span><span class="unknown">.</span><span class="me0">upcase</span><span class="unknown">!
render </span><span class="kw4">:show</span></pre>
<br />
<hr />
<h2>
<code>attribute=(value)</code> method always returns passed <code>value</code>, regardless of method return value</h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqu"><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> </span><span class="kw1">self</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown">=</span><span class="br0">(</span><span class="unknown">value</span><span class="br0">)</span><span class="unknown">
</span><span class="kw3">@foo</span><span class="unknown"> = value
</span><span class="kw1">return</span><span class="unknown"> </span><span class="st0">'OK'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown"> = </span><span class="nu0">3</span><span class="unknown"> </span><span class="co1"># => 3</span></pre>
<small>(Note that the assignment method <code>bar=</code> returns <code>3</code> even though we explicitly <code>return 'OK'</code> at the end of its body.)</small><br />
<b><br /></b>
<b>Good practice</b><br />
Never rely on anything that happens inside assignment method, eg. in conditional statements like this:<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqv"><span class="kw2"> puts</span><span class="unknown"> </span><span class="st0">'Assigned'</span><span class="unknown"> </span><span class="kw1">if</span><span class="unknown"> </span><span class="br0">(</span><span class="kw3">Foo</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown"> = </span><span class="nu0">3</span><span class="br0">)</span><span class="unknown"> == </span><span class="st0">'OK'</span><span class="unknown"> </span><span class="co1"># => nil</span></pre>
This will obviously not work.<br />
<small> More: <a href="http://stackoverflow.com/questions/6366834/ruby-define-operator-why-cant-control-return-value" target="_blank" title="ruby, define []= operator, why can’t control return value?">ruby, define []= operator, why can’t control return value?</a></small><br />
<br />
<hr />
<h2>
<code>private</code> will NOT make your <code>self.method</code> private</h2>
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqw"><span class="kw1">class</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
private
</span><span class="kw1">def</span><span class="unknown"> </span><span class="kw1">self</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown">
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="st0">'Not-so-private class method called'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown"> </span><span class="co1"># => "Not-so-private class method called"</span></pre>
<small>(Note that if the method were private, <code>Foo.bar</code> would raise <code>NoMethodError</code>.)</small><br />
<b><br /></b>
<b>Good practice</b><br />
In order to make your class method private, you have to use <code>private_class_method :method_name</code> or put your private class method inside <code>class << self</code> block:<br />
<br />
<pre class="standardEnlighterJS EnlighterJSRendered" id="EnlighterJS_hsv8hlqx"><span class="kw1"> class</span><span class="unknown"> </span><span class="kw3">Foo</span><span class="unknown">
</span><span class="kw1">class</span><span class="unknown"> << </span><span class="kw1">self</span><span class="unknown">
private
</span><span class="kw1">def</span><span class="unknown"> bar
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="st0">'Class method called'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
</span><span class="kw1">def</span><span class="unknown"> </span><span class="kw1">self</span><span class="unknown">.</span><span class="me0">baz</span><span class="unknown">
</span><span class="kw2">puts</span><span class="unknown"> </span><span class="st0">'Another class method called'</span><span class="unknown">
</span><span class="kw1">end</span><span class="unknown">
private_class_method </span><span class="kw4">:baz</span><span class="unknown">
</span><span class="kw1"> end</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">.</span><span class="me0">bar</span><span class="unknown"> </span><span class="co1"># => NoMethodError: private method `bar' called for Foo:Class</span><span class="unknown">
</span><span class="kw3">Foo</span><span class="unknown">.</span><span class="me0">baz</span><span class="unknown"> </span><span class="co1"># => NoMethodError: private method `baz' called for Foo:Class</span></pre>
<small>More: <a href="http://stackoverflow.com/questions/4952980/creating-private-class-method" target="_blank" title="creating private class method">creating private class method</a></small><br />
<br />
<hr />
<h2>
I ain’t afraid of no Ruby Gotchas</h2>
Ruby gotchas listed above may not look like a big deal, and at first
sight they may seem they are simple matter of aesthetics or conventions.<br />
<br />
Trust me – if you don’t deal with them, eventually they will lead to some nasty headaches during Ruby on Rails development. And will cause heartbreak. Because you’ll fall out of love with Ruby. And then you’ll stay alone. Forever... ;)</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-62728780862353605462014-01-20T19:01:00.001+05:302014-01-21T11:08:55.705+05:30Executing Commands in Ruby<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Ruby allows many different ways to execute commands or sub-processes. Here I will list few of them<br />
1) Backtick<br />
2) system<br />
3) exec<br />
4) sh<br />
5) popen3<br />
<br />
<b>Backtick ( </b>`cmd` ) :<br />
<b><br /></b><span style="color: #666666;">
backtick returns the output of the <i>cmd </i> in a subshell.</span><br />
<br />
<span style="color: #3d85c6;"><span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;">output = `ls`
</span><span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;">puts "output is #{output}"</span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><br /></span>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span style="color: black; font-family: 'Times New Roman'; font-size: small; line-height: normal; white-space: normal;">Result of the above code is </span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span style="color: black; font-family: 'Times New Roman'; font-size: small; line-height: normal; white-space: normal;"><br /></span></span>
<span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="line" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span><span style="color: #93a1a1;"> </span><span style="color: #3d85c6;"><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ruby</span> <span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span></span><span style="color: #3d85c6;"><span class="line" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span> <span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">is</span> <span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">amit</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">txt</span>
</span><span class="line" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span></span></span></span><br />
<span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span style="color: #3d85c6;"><span class="line" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></span></span></span>
<span style="background-color: white; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px;">Here the backtick operation forks the master process and the operation is executed in a new process. However this is a blocking operation. The main application waits until the result of backtick operation completes.</span><br />
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
If there is an exception in the sub-process then that exception is given to the main process and the main process might terminate if exception is not handled.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
In the following case I am executing <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">xxxxx</code> which is not a valid executable name.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">outut</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="sb" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">`xxxxxxx`</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"output is </span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">#{</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span style="font-variant: inherit;">Result of above code is : </span></div>
<div style="background-color: white; border: 0px; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ruby</span> </span><span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span style="color: #268bd2;">test</span><span class="o" style="border: 0px; color: #93a1a1; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span><span style="color: #93a1a1;">
</span></span><span class="line" style="border: 0px; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #268bd2;">test</span></span><span class="o" style="border: 0px; color: #93a1a1; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span><span class="p" style="border: 0px; color: #93a1a1; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="ss" style="border: 0px; color: #93a1a1; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:in</span><span style="color: #93a1a1;"> </span><span class="sb" style="border: 0px; color: #93a1a1; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">``</span><span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">': No such file or directory - xxxxxxx (Errno::ENOENT)</span></span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> from test.rb:1:in `<main>'</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span style="font-variant: inherit;">Notice that </span><code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-variant: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">puts</code><span style="font-variant: inherit;"> was never executed because the backtick operation raised exception. </span>To check the status of the backtick operation you can execute <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">$?.success?</code></div>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="sb" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">`ls`</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"output is </span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">#{</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="vg" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$?</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">success?</span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></span>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="background-color: white; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;">Notice that the last line of the result contains </span><code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline; white-space: normal;">true</code><span style="background-color: white; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;"> because the backtick operation was a success.</span></span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="background-color: white; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;"><br /></span></span></span>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ruby</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">main</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">is</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">lab</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">main</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">true</span></span></span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></span></span></span>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal; white-space: normal;"><br /></b></span></span></span></span>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal; white-space: normal;">system : </b></span></span></span></span><br />
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal; white-space: normal;"><br /></b></span></span></span></span>
<br />
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<i>system</i> behaves a bit like <i>backtick</i> operation. However there are some differences.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
First let’s look at similarities.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Just like <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">backtick</code>, <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> is a blocking operation. You can get the result of the operation using <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">$?.success?</code> . <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code>operations are also executed in a subshell.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Now the differneces between <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">backtick</code> and <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> .</div>
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="kp" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"></span></span><br />
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> eats up all the exceptions. So the main operation never needs to worry about capturing an exception raised from the child process.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">system</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'xxxxxxx'</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"output is </span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">#{</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Result of the above operation is given below. Notice that even when exception is raised the main program completes and the output is printed. The value of output is nil because the child process raised an exception.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ruby</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">output</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">is</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Another difference is that <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> returns true if the command was successfully performed ( exit status zero ) . It returns false for non zero exit status. Returns nil if command execution fails.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal;">exec : </b></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<i>exec</i> replaces the current process by running the external command. Let’s see an example.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Here I am in irb and I am going to execute <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">exec('ls')</code>.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">irb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">e1</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">9</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">3</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">-</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">p194</span> <span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="mo" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">001</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">></span> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">exec</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'ls'</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">amit</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">rb</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;">
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">amitk</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">~</span><span class="sr" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">/dev/</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">lab</span> <span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">9</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">3</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">$</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
I see the result but since the irb process was replaced by the <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">exec</code> process I am no longer in <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">irb</code> .</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Behind the scene both <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> and <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">backtick</code> operations use <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">fork</code> to fork the current process and then they execute the given operation using <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">exec</code> .</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="err" style="background-image: none !important; border: 0px; color: rgb(220, 50, 47) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Since <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">exec</code> replaces the current process it does not return anything if the operation is a success. If the operation fails then `SystemCallError is raised.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal;">sh : </b></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<i>sh</i> actually calls <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">system</code> under the hood. However it is worth a mention here. This method is added by <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">FileUtils</code> in <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">rake</code>. It allows an easy way to check the exit status of the command.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">require</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'rake'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">sh</span> <span class="sx" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">%w(xxxxx)</span> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ok</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">res</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">if</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">!</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ok</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">abort</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'the operation failed'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b style="color: black; font-family: 'Times New Roman'; font-size: medium; line-height: normal; white-space: normal;">popen3 : </b></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
If you are going to capture <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">stdout</code> and <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">stderr</code> then you should use <a href="http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen3" style="-webkit-transition: color 0.3s; border: 0px; color: #0181eb; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; transition: color 0.3s; vertical-align: baseline; white-space: pre-wrap; word-wrap: break-word;">popen3</a> since this method allows you to interact with <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">stdin</code>, <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">stdout</code> and <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">stderr</code> .</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
I want to execute <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">git push heroku master</code> programmatically and I want to capture the output. Here is my code.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">require</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'open3'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'git push heroku master'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Open3</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">popen3</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdin</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stderr</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">wait_thr</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"stdout is:"</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">+</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">read</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"stderr is:"</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">+</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stderr</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">read</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;">And here is the output. It has been truncated since rest of output is not relevant to this discussion.</span></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span> <span class="ss" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">is</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stderr</span> <span class="ss" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">is</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Heroku</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">receiving</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">push</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Ruby</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">/</span><span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Rails</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">app</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">detected</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Installing</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">dependencies</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">using</span> <span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Bundler</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">version</span> <span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">2</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span></span></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;">Lets see an example of capturing streaming output.</span></span></span></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="mi" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">require</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'open3'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'ping www.google.com'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Open3</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">popen3</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdin</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stderr</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">wait_thr</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">while</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">line</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">gets</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">puts</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">line</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span>
</span><span class="line" style="border: 0px; color: #93a1a1; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span></span></span></span></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
In the above case you will get the output of ping on your terminal as if you had typed <code style="background-color: #eeeeee; border-bottom-left-radius: 0.4em; border-bottom-right-radius: 0.4em; border-top-left-radius: 0.4em; border-top-right-radius: 0.4em; border: 0px; color: #666666; display: inline-block; font-size: 16px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.5em; margin: -1px 0px; padding: 0px 2px; vertical-align: baseline;">ping www.google.com</code> on your terminal .</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
Now let’s see how to check if command succeeded or not.</div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">require</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'open3'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="s1" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'ping www.google.com'</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="no" style="border: 0px; color: rgb(181, 137, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Open3</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">popen3</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdin</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stdout</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">stderr</span><span class="p" style="border: 0px; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">wait_thr</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">|</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">exit_status</span> <span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">wait_thr</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">value</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">unless</span> <span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">exit_status</span><span class="o" style="border: 0px; font-family: inherit; font-variant: inherit; font-weight: bold !important; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">success?</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="nb" style="border: 0px; color: rgb(133, 153, 0) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">abort</span> <span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"FAILED !!! </span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">#{</span><span class="n" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">cmd</span><span class="si" style="border: 0px; color: rgb(38, 139, 210) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="s2" style="border: 0px; color: rgb(42, 161, 152) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"> <span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span>
</span><span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">end</span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;">Hope this post helps in understanding the basics of executing commands from our program.</span></span></span></div>
<div style="background-color: white; border: 0px; color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; margin-bottom: 1.5em; padding: 0px; vertical-align: baseline;">
<span class="line" style="border: 0px; color: #93a1a1; font-family: monospace; font-size: 16px; line-height: 23.1875px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: pre;"><span class="k" style="border: 0px; color: rgb(203, 75, 22) !important; font-family: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #555555; font-family: arvo; font-size: 18px; line-height: 30px; white-space: normal;">Happy Coding... :)</span></span></span></div>
</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-42278693697025104132013-11-28T17:06:00.001+05:302013-12-16T16:37:43.262+05:30Debugging tools and techniques in development mode<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="comments-link">
</div>
I’ve been spending a lot of time answering questions on <a href="http://stackoverflow.com/users/952089/amit-karsale" target="_blank">Stack Overflow</a>
lately. From what I read, most of the questions come from unexperienced
developers(which am also..).<br />
<br />
As practising good debugging techniques and tools helps you solve your problem at once. Here are few of the mentions which I personally follow.<br />
<br />
<b>The basics</b> :<br />
<br />
When working with Ruby on Rails you will have to learn a lot of stuff
to master debugging your own application. There are so many moving
parts in a Rails app, it’s not just <span class="caps">MVC…</span><br />
<ul>
<li>Views (Partials, Layouts, Templating-Engines)</li>
<li>Controllers (Actions, Filter)</li>
<li>Models (Relations, <span class="caps">AREL,</span> Callbacks, Database-Systems)</li>
<li>Mailers</li>
<li>Routes (REST, <span class="caps">HTTP</span>-Verbs, Constraints)</li>
<li>Environments and Initializers</li>
<li>Caching (Redis, Memcached)</li>
<li>Assets (CSS, <span class="caps">SASS,</span> JavaScript, CoffeeScript, Pipelining)</li>
<li>Bundler and dependency management</li>
<li>Tests (RSpec, Capybara)</li>
<li>gems, plugins and engines used in the app</li>
</ul>
<br />
Because of this overwhelming complexity, it is very difficult to keep in mind all the stuff and the flow of actions happening. Better way is to say <b>Halt </b>and let me see what are you upto.<br />
<h3>
Using rake:</h3>
Rake was intended as a Make for Ruby. It’s basically a build-tool
that handles tasks. So make sure you know when it can be useful for you! The basic
commands are <span style="background-color: #999999;"> <code>rake -T</code> <span style="background-color: white;"> </span></span>to get a list of <i>public</i> commands and <span style="background-color: #999999;"> <code>`rake -D</code>` </span> to get the full description of the tasks. If one of the commands fail, you can pass the <code>--trace</code> option to see what rake is doing under the hood and find possible error causes.<br />
Here is a list of useful commands that I use when running into strange errors during development:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="bash"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
</pre>
</td><td class="de1"><pre class="de1">rake routes
rake middleware
rake assets:clobber
rake assets:clean
rake tmp:<span class="kw2">clear</span>
rake log:<span class="kw2">clear</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<h3>
Reading stack-traces:</h3>
Reading traces means you have to be <i>Bond - James Bond</i> by nature B-)<br />
Traces leads you to the root cause or atleast to the cause which is making the code fail (thats what detective do in their day-to-day life). Providing meaningful error
messages is one of most neglected parts in writing maintainable
software. You should always <b>read</b> the errors and stack-traces because they contain helpful information like the <i>source file</i> and the <i>line number</i>
where an error was caused. It even provides information about the
context, like the calling object and the call-stack when the error
happened.<br />
Let me give you <a href="http://stackoverflow.com/questions/19317128/why-i-am-getting-undefined-local-variable-or-method-role-in-cucumber-test-case/19319592">an example exception</a>:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="bash"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
</pre>
</td><td class="de1"><pre class="de1">undefined <span class="kw3">local</span> variable or method <span class="sy0">`</span>role<span class="st_h">' for #<Cucumber::Rails::World:0xc4722f8> (NameError)</span>
<span class="st_h"> ./features/step_definitions/event_steps.rb:10:in `create_visitor'</span>
.<span class="sy0">/</span>features<span class="sy0">/</span>step_definitions<span class="sy0">/</span>event_steps.rb:<span class="nu0">14</span>:<span class="kw1">in</span> <span class="sy0">`</span>create_user<span class="st_h">'</span>
<span class="st_h"> ./features/step_definitions/kid_steps.rb:107:in `/^I am exists as a parent$/'</span>
features<span class="sy0">/</span>manage_kids.feature:<span class="nu0">11</span>:<span class="kw1">in</span> <span class="sy0">`</span>And I am exists <span class="kw2">as</span> a parent<span class="st_h">'</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
What I can see at a glance: <br />
<ul>
<li>it’s about cucumber</li>
<li>we are in a step definition</li>
<li>the context object is of class <code>Cucumber::Rails::World</code></li>
<li>there should be a <code>role</code> but it is not available</li>
<li>calling <code>create_visitor</code> caused this error</li>
<li>the source of <code>create_visitor</code> is in <code>event_steps.rb</code> on line 10</li>
</ul>
The source code that matches this error is this:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="ruby"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
</pre>
</td><td class="de1"><pre class="de1"><span class="co1"># event_steps.rb</span>
<span class="kw1">def</span> create_visitor
<span class="re1">@visitor</span> <span class="sy0">||</span>= <span class="br0">{</span>
<span class="re3">:email</span> <span class="sy0">=></span> <span class="st0">"user@example.com"</span>,
<span class="re3">:password</span> <span class="sy0">=></span> <span class="st0">"test123"</span>,
<span class="re3">:password_confirmation</span> <span class="sy0">=></span> <span class="st0">"test123"</span>,
<span class="re3">:role</span> <span class="sy0">=></span> Role.<span class="me1">find_by_name</span><span class="br0">(</span>role.<span class="me1">to_s</span><span class="br0">)</span> <span class="co1"># this is line 10</span>
<span class="br0">}</span>
<span class="kw1">end</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
It is just an excerpt of the code, so it’s a good idea to add real
line-numbers or an anchor so other people know where we are. In this
example, this would not even be necessary, because <i>role</i> is only
called once. Without knowing anything about the source, I can
immediately see that the author wanted to use something that is not
there.<br />
So possible solutions to this might be to use <i>@role</i> or maybe <i>@visitor.role</i> or to have a method called <i>role</i>
or pass it in as a parameter to that method. If the test has run
successfully before doing any changes, the error was probably introduced
by some changes you did and should be easy to find.<br />
<i>So always make sure your test suite passes before writing new code</i>.<br />
<br />
<h4>
Side Note:</h4>
Never throw away exception information unless there is a good reason!
Swallowing exceptions causes a lot of pain for people that have to
maintain the running application, so please at least log the error
message when you <code>rescue</code> from something.<br />
<br />
<h3>
Reading code:</h3>
One of the key skills for writing code is reading code in the first
place. Whether it is code I wrote some time ago, code of my coworkers or
library code. It’s super important to understand the code at hand and
the code that my application is executing. Compared to other languages,
where one has to deal with compiled sources, it’s incredibly easy to
have a look at ruby sources. I often run <code>bundle open some_gem</code> to look at the source or when you are not using bundler run <code>gem env</code> and look at <code>GEM PATHS</code> where your gems are installed<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="bash"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
</pre>
</td><td class="de1"><pre class="de1">gem <span class="kw2">env</span>
RubyGems Environment:
- RUBYGEMS VERSION: 2.1.5
- RUBY VERSION: 2.0.0 <span class="br0">(</span><span class="nu0">2013</span>-06-<span class="nu0">27</span> patchlevel <span class="nu0">247</span><span class="br0">)</span> <span class="br0">[</span>x86_64-darwin12.4.0<span class="br0">]</span>
<span class="br0">[</span>...<span class="br0">]</span>
- GEM PATHS:
- <span class="sy0">/</span>Users<span class="sy0">/</span>paule<span class="sy0">/</span>.rbenv<span class="sy0">/</span>versions<span class="sy0">/</span>2.0.0-p247<span class="sy0">/</span>lib<span class="sy0">/</span>ruby<span class="sy0">/</span>gems<span class="sy0">/</span>2.0.0
<span class="br0">[</span>...<span class="br0">]</span>
subl ~<span class="sy0">/</span>.rbenv<span class="sy0">/</span>versions<span class="sy0">/</span>2.0.0-p247<span class="sy0">/</span>lib<span class="sy0">/</span>ruby<span class="sy0">/</span>gems<span class="sy0">/</span>2.0.0<span class="sy0">/</span>gems<span class="sy0">/</span>rails-4.0.0<span class="sy0">/</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
<h2>
Debugging the running server:</h2>
I sit in front of
the browser and reload the page to see if something happens… Unluckily
it does not work the way I expected, so what to do next? <br />
<h3>
Find it in the logs..</h3>
When I am working on a Ruby or Rails application i like to run stuff in <a href="http://www.ubuntugeek.com/terminator-multiple-gnome-terminals-in-one-window.html" target="_blank">terminator</a> with split views. One session for running spec, rails or the console and one session with the corresponding logs with <code>tail -f log/development.log</code><br />
<br />
<a href="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-10.22.00.png"><img alt="Screen Shot 2013-10-16 at 10.22.00" class="aligncenter size-large wp-image-825" src="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-10.22.00-818x1024.png" height="782" width="625" /></a><br />
<br />
Logs help with a lot of stuff and are a powerful tool to debug your running code. Things that can be extracted from the logs:<br />
<ul>
<li>direct error information
<ul>
<li>error-messages</li>
<li>complete stack-traces</li>
<li>logged warnings or errors (attribute accessible warnings, deprecations)</li>
</ul>
</li>
<li>context information
<ul>
<li>request parameters</li>
<li>request types (html, json, xhr…)</li>
<li>response codes</li>
<li>sql queries</li>
<li>rendered views and partials</li>
<li>callback informations</li>
</ul>
</li>
</ul>
Since Rails introduced the asset pipeline, logs became pretty
useless, because they were cluttered with asset calls. Disabling those
logs does not need any ugly middleware monkey patches anymore because
you can use the <a href="https://github.com/evrone/quiet_assets">quiet_assets gem</a> for this purpose!<br />
One thing to pimp the logs to be even more useful is to use the <a href="http://weblog.rubyonrails.org/2012/1/20/rails-3-2-0-faster-dev-mode-routing-explain-queries-tagged-logger-store/">tagged logger</a> that was introduced in Rails 3.2. It allows you to put even more context information into the log like the session-id and request-id:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="ruby"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
</pre>
</td><td class="de1"><pre class="de1"><span class="co1"># application.rb</span>
config.<span class="me1">log_tags</span> = <span class="br0">[</span>
<span class="re3">:host</span>,
<span class="re3">:remote_ip</span>,
<span class="kw3">lambda</span> <span class="br0">{</span> <span class="sy0">|</span>request<span class="sy0">|</span> <span class="st0">"#{request.uuid}"</span><span class="br0">[</span><span class="nu0">0</span>..<span class="nu0">15</span><span class="br0">]</span> <span class="br0">}</span>,
<span class="kw3">lambda</span> <span class="br0">{</span> <span class="sy0">|</span>request<span class="sy0">|</span> <span class="st0">"#{request.cookie_jar["</span>_on_ruby_session<span class="st0">"]}"</span><span class="br0">[</span><span class="nu0">0</span>..<span class="nu0">15</span><span class="br0">]</span> <span class="br0">}</span>,
<span class="br0">]</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<h3>
Quick inspects:</h3>
There are a couple of “workflows” which I tend to use in my everyday debugging, starting with simply adding <code>p some_object</code>, <code>puts some_object.inspect</code> or <code>logger.debug some_object</code> calls randomly to the code. For view templates i use <code>= debug(some_object)</code>.<br />
<br />
This helps in about 50% of the error cases because it gives me enough context to find the problem quickly.<br />
<br />
Another thing is putting a <code>raise some_object.to_s</code> into
the code. This is especially helpful if I want to find out when and how
some part of the code is executed. Callbacks are a good example where
this is a nice shorthand method for debugging.<br />
<br />
<h3>
Insight tools:</h3>
A running Rails server can provide a lot of useful information for
debugging, especially if you curry it with the right helpers. Rails 4
already comes with some better error reporting and a route helper that
can be accessed through navigating to an unknown route. I use the <code>/routes</code> path for this purpose:<br />
<br />
<br />
<a href="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-13.04.18.png"><img alt="Screen Shot 2013-10-16 at 13.04.18" class="aligncenter size-large wp-image-834" src="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-13.04.18-1024x529.png" height="322" width="625" /></a><br />
<br />
This functionality can be improved with a nice UI and a mini Rails console running in your browser if you add <a href="https://github.com/charliesome/better_errors">Better Errors</a> and the <a href="https://github.com/banister/binding_of_caller">Binding of Caller</a>
gem. These tools allow you to dive right into the error context and
find out what might have went wrong. Combining this with raising my own
errors gives me a lot of flexibility to quickly get to the point where I
assume that something fishy is going on in the code.<br />
<br />
<br />
<a href="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-13.02.46.png"><img alt="Screen Shot 2013-10-16 at 13.02.46" class="aligncenter size-large wp-image-832" src="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-13.02.46-1024x547.png" height="333" width="625" /></a><br />
<br />
When you are doing a lot of ActionMailer related stuff, you probably want to install the <a href="https://github.com/ryanb/letter_opener">Letter Opener gem</a> to develop and inspect your E-Mails.<br />
<h2>
The debugger:</h2>
Having tools like Better Errors is super nice, but they are only
suited for usage in the Browser. This is not always possible and I fall
back to real debuggers that allow for moving around in the call-stack and inspecting objects at runtime.<br />
The <a href="http://bashdb.sourceforge.net/ruby-debug.html">ruby-debug gem</a> was the go-to-guy for a long time in Ruby land. It was a <span class="caps">PITA </span>to get this up and running in latest Ruby versions. That’s why I use the <span class="caps">IRB </span>replacement <a href="https://github.com/pry/pry">pry</a> with a lot of extensions like <a href="https://github.com/nixme/pry-debugger">pry-debugger</a> or <a href="https://github.com/nixme/pry-nav">pry-nav</a>. If you want to hook into a remote Rails process (ie. running rails via foreman) you can hook into it with <a href="https://github.com/Mon-Ouie/pry-remote">pry-remote</a>. Have a look at the <a href="http://railscasts.com/episodes/280-pry-with-rails">Pry Railscast</a> for more information.<br />
<h2>
Debugging through the console:</h2>
The console is very important for me when writing new or changing
existing code. I often create new finders and scopes in the console
directly. A very important command in the console is <code>reload!</code>. It reloads all the code in the current session, similar to what happens when you hit <code>CTRL+R</code> in the browser:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="ruby"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
</pre>
</td><td class="de1"><pre class="de1">reload!; something_i_changed_in_the_editor.<span class="me1">check_if_it_works</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
A thing that I find very helpful when stuck in a debugging session is the <code>method.source_location</code> functionality introduced in Ruby 1.9. It allows me to see where a method is defined:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="ruby"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
</pre>
</td><td class="de1"><pre class="de1">user.<span class="me1">method</span><span class="br0">(</span><span class="re3">:url</span><span class="br0">)</span>.<span class="me1">source_location</span>
<span class="sy0">=></span> <span class="br0">[</span><span class="st0">"/Users/amit/Documents/rails/on_ruby/app/models/user.rb"</span>, <span class="nu0">39</span><span class="br0">]</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<h3>
</h3>
I like to use <a href="https://github.com/rweng/pry-rails">pry-rails</a> as my default Rails Console. It enables me to browse source code, inspect objects and do a lot of other crazy stuff. Just type <code>help</code> in a pry session to see what commands are available. My favorite command is <code>wtf?</code> which shows the last raised exception and stack-trace.<br />
<h2>
Debugging through testing:</h2>
I love test driven development! It is an everyday part of my programming routine. The only thing that I hate when doing <span class="caps">TDD </span>in Rails is the slow feedback loop. I don’t like using tools like <a href="https://github.com/sporkrb/spork-rails">spork</a> or <a href="https://github.com/burke/zeus">zeus</a> as they introduce a lot of complexity and make debugging even harder.<br />
So my approach to get away with minimal turnaround time is to start my new expectations by writing somthing like this:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="ruby"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
</pre>
</td><td class="de1"><pre class="de1">describe User <span class="kw1">do</span>
it <span class="st0">"does crazy stuff"</span> <span class="kw1">do</span>
pry
<span class="kw1">end</span>
<span class="kw1">end</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
This rspec expectation will just open a new pry session where I can
start coding right away, exploring my test with direct feedback from
executing the code I want to test. This eliminates all the iteration-
and startup time when running red/green cycles. It is especially useful
when I don’t know exactly how something should function in the first
place…<br />
<br />
<h3>
Debugging capybara:</h3>
The last pattern is super useful when writing <a href="https://github.com/jnicklas/capybara">acceptance tests in capybara</a>!
I always forget how to use capybara expectations and matchers, because I
write only a few of those tests. This is mainly due to <a href="http://blog.codeclimate.com/blog/2013/10/09/rails-testing-pyramid/">the test pyramid</a> and me being a backend developer.<br />
There are also a lot more moving parts in acceptance tests,
especially when running JavaScript tests in a headless browser. It’s
super useful when you can just check <code>page.body</code> or temper with X-PATH expressions directly in the pry session.<br />
If there is some strange behavior during test execution, I just put a breakpoint somewhere in the test and call <code>save_and_open_page</code> or even better: print out <code>current_url</code> and open the running test instance directly in the browser!<br />
<h2>
Other helpful tools:</h2>
The Rails ecosystem comes with a lot of tools for debugging, but it can be enhanced even further:<br />
<h3>
Rails Panel:</h3>
If you are using Google Chrome like me, you can use the <a href="https://chrome.google.com/webstore/detail/railspanel/gjpfobpafnhjhbajcjgccbbdofdckggg" target="_blank">Rails Panel plugin</a> in combination with the <a href="https://github.com/dejan/rails_panel/tree/master/meta_request">Meta Request gem</a> to get a Rails Developer Console in your browser. Super sweet!<br />
<br />
<a href="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-11.21.51.png"><img alt="Screen Shot 2013-10-16 at 11.21.51" class="aligncenter size-large wp-image-828" src="http://nofail.de/wp-content/uploads/2013/10/Screen-Shot-2013-10-16-at-11.21.51-1024x583.png" height="355" width="625" /></a><br />
<h3>
</h3>
<h3>
cURL:</h3>
<h3>
</h3>
If I want to look into response headers or other request/response specific information, Use <code>curl</code> in addition to the Chrome Developer Tools:<br />
<br />
<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-wrapper clearfix bwp-syntax-simple">
<table class="bash"><tbody>
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
</pre>
</td><td class="de1"><pre class="de1">curl <span class="re5">-I</span> <span class="st0">"http://hamburg.onruby.dev:5000/"</span>
HTTP<span class="sy0">/</span><span class="nu0">1.1</span> <span class="nu0">200</span> OK
<span class="br0">[</span>...<span class="br0">]</span></pre>
</td></tr>
</tbody></table>
</div>
</div>
<br />
It is even possible to use the “Copy as cURL” option in the Chrome
Developer Tools to get all the parameters necessary to redo some
request!<br />
<br />
<h3>
Navicat:</h3>
<h3>
</h3>
Use <a href="http://www.navicat.de/">Navicat</a> as a multi db
frontend. It allows to inspect what is going on in the database,
check query results, indices and create data on the fly. Always have a
good client for your data stores available!<br />
<br />
<h3>
<span class="caps">GIT:</span></h3>
<h3>
<span class="caps"> </span></h3>
<a href="http://git-scm.com/book">Havent used git</a>, but want to. It’s not the most
self explanatory tool on the planet but it helps when
debugging applications. Just one example here is using <code>git bisect</code>.
It executes a command for a range of commits, so that it is possible to
find out which commit introduced a problem.<br />
<br />
<h2>
Resources worth reading:</h2>
Here are some websites that I keep referring to in a lot of my
answers on stackoverflow. Go ahead and check all of them, really, I mean
it!<br />
<ul>
<li><a href="http://guides.rubyonrails.org/">Rails Guides</a> (<a href="http://edgeguides.rubyonrails.org/">Edge Guides</a>)</li>
<li><a href="http://www.ruby-doc.org/docs/ProgrammingRuby/">The Ruby Pickaxe</a></li>
<li><a href="http://railscasts.com/">RailsCasts</a></li>
<li><a href="http://apidock.com/rails"><span class="caps">API</span> Dock</a></li>
<li><a href="https://www.ruby-toolbox.com/">Ruby-Toolbox</a></li>
</ul>
</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-29444836063794702272013-11-25T10:20:00.001+05:302013-11-25T10:20:08.655+05:30Installing Ruby, RVM, Rails, Mysql, Mysql-Query-Browser,Chrome browser, Aptana, Sublime all in one<div dir="ltr" style="text-align: left;" trbidi="on">
First of all, we're going to run `<i><code>sudo apt-get update</code></i>` so
that we have the latest sources on our box so that we don't run into any
package-related issues, such as not being able to install some
packages. <br />
Next, we're going to install <code>curl</code>, which we'll use to fetch the RVM script:<br />
<div class="highlight">
<pre><code class="text language-text" data-lang="text"> </code></pre>
<pre><code class="text language-text" data-lang="text"><i>`sudo apt-get install curl</i>`</code></pre>
<pre><code class="text language-text" data-lang="text"> </code></pre>
<h3>
RVM</h3>
RVM is a <i>Ruby Version Manager</i> created by
Wayne E. Seguin and is extremely helpful for installing and managing
many different versions of Ruby all at once. Sometimes you could be
working on a project that requires an older (1.8.7) version of Ruby but
also need a new version (2.0.0) for one of your newer projects. This is a
problem that RVM solves beautifully. <br />
Another situation could be that you want to have different sets of
gems on the same version of Ruby but don't want to have to do deal with
Gem Conflict Hell. RVM has <i>gemsets</i> for this.<br />
<br />
With <i><code>curl</code></i> installed we'll be able to install RVM with this command:
<br />
<div class="highlight">
<pre><code class="text language-text" data-lang="text"> </code></pre>
<pre><code class="text language-text" data-lang="text">`<i>curl -L get.rvm.io | bash -s stable --auto</i>`
</code></pre>
</div>
<br />
The beautiful part of this is that it installs RVM and Ruby to our
home directory, providing a sandboxed environment just for us.<br />
Then we'll need to reload the <i><code>~/.bash_profile</code></i> file which we can do with this small command:<br />
<br />
<div class="highlight">
<pre><i><code class="text language-text" data-lang="text">. ~/.bash_profile</code></i></pre>
<pre><code class="text language-text" data-lang="text"> </code></pre>
</div>
The next command we run will tell us what other packages we need to install for Ruby to work:<br />
<br />
<i>`<code class="text language-text" data-lang="text">rvm requirements`</code></i></div>
<div class="highlight">
</div>
<div class="highlight">
A couple of things to note in this is that the <i><code>build-essential</code></i> package is
installed, which will install all the essential build tools for Ubuntu, so
we'll be able to download and compile Ruby, amongst other things. <br />
This will also install <i>Git</i>, which is a version
control system tool that you should be using if you're not already. This is
used by RVM to fetch the latest source for some versions of Ruby.<br />
<br />
These packages will lessen the pain when we're working with Ruby. For example, the <i><code>libssl-dev</code></i> package will make OpenSSL support in Ruby work, <code>libsqlite3-0</code> and <i><code>libsqlite3-dev</code></i> are required for the <i><code>sqlite3-ruby</code></i> gem and the <i><code>libxml2-dev</code></i> and <i><code>libxslt-dev</code></i> packages are required for the <i><code>nokogiri</code></i> gem.<br />
<br />
Install all these packages now using this command:<br />
<br />
<div class="highlight">
<pre><i><code class="text language-text" data-lang="text">`sudo apt-get install build-essential openssl libreadline6 libreadline6-dev \
curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 \
libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison \
subversion pkg-config`</code></i></pre>
<pre><i><code class="text language-text" data-lang="text"> </code></i></pre>
<pre><i><code class="text language-text" data-lang="text"> </code></i></pre>
<h3>
Ruby</h3>
With RVM and these packages we can install Ruby 1.9.3 or 2.0.0:<br />
<div class="highlight">
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i>`rvm install 2.0.0</i>`</code></pre>
<pre><code class="text language-text" data-lang="text"> </code></pre>
</div>
Once it's done, we'll have
Ruby 2.0.0 installed. To begin using it we can use this lovely command:<br />
<div class="highlight">
<pre><code class="text language-text" data-lang="text"> </code></pre>
<pre><code class="text language-text" data-lang="text"><i>`rvm use 2.0.0`</i>
</code></pre>
</div>
<pre><i><code class="text language-text" data-lang="text"> </code></i></pre>
</div>
<br />
<pre><code class="text language-text" data-lang="text"> </code><span style="-webkit-text-stroke-width: 0px; background-color: #efefef; color: #444444; display: inline !important; float: none; font-family: Georgia, sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">Are we using 2.0.0? You betcha:</span></pre>
<pre><code class="text language-text" data-lang="text"> `<i>ruby -v`</i>
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]</code></pre>
<pre><code class="text language-text" data-lang="text"> </code></pre>
<pre><span style="background-color: white;"><span style="color: #444444; display: inline ! important; float: none; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">To make this as a default ruby use the following command: </span></span><code class="text language-text" data-lang="text"><span style="background-color: white;"><span style="color: #444444; display: inline ! important; float: none; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </span></span></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i>`rvm --default use 2.0.0`</i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> <span style="-webkit-text-stroke-width: 0px; background-color: #efefef; color: #444444; display: inline !important; float: none; font-family: Georgia, sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">As an additional side-note: Users can, and should, use a gemset when possible so that they don't pollute their 'default' which is what is selected when a gemset is not specified in either a project's .rvmrc, or at the command-line. Each installed Ruby has a '@global' gemset. This is used to share gems with other gemsets created under that specific Ruby, and with the 'default' gemset. This can be selected by running 'rvm gemset use global' and then installing the gems you wish to share to other gemsets including 'default'. You can, of course simply install in each gemset but this will cause needless duplication and use up more disk-space and bandwidth.</span> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">Rails</span></h3>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">Now that RVM and a version of Ruby is installed, we can install Rails. Because RVM is installed to our home directory, we don't need to use that nasty<span class="Apple-converted-space"> </span><code style="border-radius: 5px; border: 1px solid rgb(222, 222, 222); color: #538b31; padding: 2px;">sudo</code><span class="Apple-converted-space"> </span>to install things; we've got write-access! To install the Rails gem we'll run this command:</span></div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: #efefef; color: #444444; font-family: Georgia, sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<pre style="background-color: #242424; background-position: initial initial; background-repeat: initial initial; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px; color: #92f857; font-family: Inconsolata, Monaco, monospace; font-size: 1.2em; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; padding: 10px;"><code class="text language-text" data-lang="text">gem install rails -v 4.0.0
</code></pre>
</div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">This will install the<span class="Apple-converted-space"> </span><code style="border-radius: 5px; border: 1px solid rgb(222, 222, 222); color: #538b31; padding: 2px;">rails</code><span class="Apple-converted-space"> </span>gem and the multitude of gems that it and its dependencies depend on, including Bundler.</span></div>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">MySQL</span></h3>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">If you're planning on using the<span class="Apple-converted-space"> </span><code style="border-radius: 5px; border: 1px solid rgb(222, 222, 222); color: #538b31; padding: 2px;">mysql2</code><span class="Apple-converted-space"> </span>gem for your application then you'll want to install the<span class="Apple-converted-space"> </span><code style="border-radius: 5px; border: 1px solid rgb(222, 222, 222); color: #538b31; padding: 2px;">libmysqlclient-dev</code><span class="Apple-converted-space"> </span>package before you do that. Without it, you'll get an error when the gem tries to compile its native extensions.</span></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<br /></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"><span style="background-color: white;"><span style="color: #444444; display: inline ! important; float: none; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">And that's it! Now you've got a Ruby environment you can use to write your (first?) Rails application in with such minimal effort.</span></span></span></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"><span style="background-color: white;"><span style="color: #444444; display: inline ! important; float: none; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> </span></span></span></div>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">Chrome Browser</span></h3>
Download the current stable version from <a href="http://www.google.com/chrome">here</a>.
After downloading the “.deb” file, open a terminal and move to the
directory where the debian file is downloaded. For example, if the file
is in Downloads use the below command to move to the Downloads
directory.<br />
<br />
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><i><span style="background-color: white;">`cd Downloads</span></i></pre>
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><span style="background-color: white;"><strong> </strong></span></pre>
After
changing the directory, use the below command to install the google
chrome from debian file. Type the password for the user when prompted.<br />
<br />
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><i><span style="background-color: white;">`sudo dpkg -i google-chrome-stable_current_i386.deb`</span></i></pre>
<h4 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"> </span><span style="font-weight: normal;">If you find any error and the installation fails because of dependency Then try using the below command, type the password for the user when prompted.</span></h4>
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><i><span style="background-color: white;">`sudo apt-get -f install</span></i><span style="font-weight: normal;"><i>`</i></span></pre>
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><span style="font-weight: normal;"><i> </i></span></pre>
<pre style="background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto auto; overflow: auto;"><span style="font-weight: normal;"><i> </i></span></pre>
<h4 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="font-weight: normal;">If you still find error in installing the pkg, try looking your `<span style="font-size: x-small;"><i>/tmp</i></span>` folder, you will find multiple pkgs installed. Choose the required one and <i><span style="font-size: x-small;">dpkg</span></i> again </span></h4>
<h4 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="font-weight: normal;">Thats it..!!</span></h4>
<h4 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="font-weight: normal;"></span></h4>
<h4 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
</h4>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">Aptana Studio 3</span></h3>
<div style="text-align: left;">
<strong>1. <span style="font-size: small;">Install the prerequisites</span></strong></div>
<div style="text-align: left;">
<br /></div>
<div class="highlight">
<pre><i><code class="text">`sudo apt-get install openjdk-7-jdk libjpeg62 libwebkitgtk-1.0-0 git-core`</code></i></pre>
<pre><code class="text"> </code></pre>
</div>
Although Aptana Studio doesn’t officially support OpenJDK, I’ve not
encountered any problems, however I’ve not done extensive testing.
Alternatively, to use the Sun JDK, do the following:<br />
<br />
<pre><code class="text">sudo apt-get install libjpeg62 libwebkitgtk-1.0-0 git-core
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer</code><span style="background-color: white;"> </span></pre>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"></span></h3>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"><i>libjpeg62</i> pkg is important, without it you will get errors.</span></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<br /></div>
<strong>2. Download Aptana Studio</strong><br />
<br />
You can download <a href="http://www.aptana.com/products/studio3/download">Aptana Studio 3 here</a>. Select the “Standalone Version” if not selected and click download.<br />
<br />
<strong>3. Extract **Aptana Studio</strong>**<br />
<br />
<div class="highlight">
<pre><code class="text">sudo unzip [name of Aptana Studio ZIP file here].zip -d /opt</code></pre>
<pre><code class="text"> </code></pre>
</div>
<strong>4. Add the menu shortcut</strong><br />
<br />
<div class="highlight">
<pre><code class="text">wget http://www.samclarke.com/wp-content/uploads/2012/04/AptanaStudio3.desktop
sudo mv AptanaStudio3.desktop /usr/share/applications/AptanaStudio3.desktop</code></pre>
<pre><code class="text"> </code></pre>
</div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;"> </span><span style="background-color: white;">And you are done..!!!</span></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">If you prefer sublime over aptana, here's how to get it on your machine.</span></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<br /></div>
<div style="color: #444444; font-family: Georgia,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
</div>
<h3 style="color: #444444; font-family: Georgia,sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 32px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="background-color: white;">Sublime</span></h3>
<div class="field field-name-body field-type-text-with-summary field-label-hidden">
<div class="field-items">
<div class="field-item even">
After reading numerous blog posts about how to install <a href="http://www.sublimetext.com/2" target="_blank" title="Sublime Text 2">Sublime Text 2</a> in Ubuntu, this is definitely the quickest way! Just paste the following lines into your Terminal:<br />
<br />
<pre><code>sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text</code></pre>
<pre><code> </code></pre>
After running this, Sublime Text 2 has been installed within the <em>/usr/lib/sublime-text-2</em> directory and can be launched from the Dashboard, or by typing <code>subl</code>, <code>sublime-text</code> or <code>sublime-text-2</code> into a Terminal window.<br />
</div>
</div>
</div>
<pre><code class="text language-text" data-lang="text"><i> </i></code></pre>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-69641288158103103982013-11-19T14:51:00.001+05:302013-11-28T14:56:38.000+05:30Regular Expression Understanding<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="big">
Stage1 </div>
<div class="big">
<br /></div>
<b>Symbol</b> <b>Explanation</b><br />
<br />
<span style="color: #cc0000;"><b>^</b></span> Start of string<br />
<span style="color: blue;"><b>$ </b></span> End of string<br />
<b>. </b> Any single character<br />
<span style="color: #38761d;"><b>+</b></span> One or more character<br />
<span style="color: #0b5394;"><b>\ </b></span> Escape Special characters<br />
<span style="color: #741b47;"><b>?</b></span> Zero or more characters<br />
<br />
Input exactly match with “abc” <br />
<div class="code">
<span style="color: #0b5394;">var</span> A = /<span style="color: #cc0000;"><b>^</b></span>abc<span style="color: blue;"><b>$</b></span>/; </div>
<br />
Input start with “abc”<br />
<div class="code">
<span style="color: #0b5394;">var </span>B = /<b><span style="color: #cc0000;">^</span></b>abc/; </div>
<br />
Input end with “abc”<br />
<div class="code">
<span style="color: #0b5394;">var </span>C = /abc<b><span style="color: blue;">$</span></b>/;</div>
<br />
Input “abc” and one character allowed Eg. abcx<br />
<div class="code">
<span style="color: #0b5394;">var </span>D = /<span style="color: #cc0000;"><b>^</b></span>abc<b>.<span style="color: blue;">$</span></b>/;</div>
<br />
Input “abc” and more than one character allowed Eg. abcxy<br />
<div class="code">
<span style="color: #0b5394;">var </span>E = /<b><span style="color: #cc0000;">^</span></b>abc<b>.<span style="color: #38761d;">+</span><span style="color: blue;">$</span></b>/;</div>
<br />
Input exactly match with “abc.def”, cause (.) escaped<br />
<div class="code">
<span style="color: #0b5394;">var </span>F = /<span style="color: #cc0000;"><b>^</b></span>abc<b><span style="color: #0b5394;">\</span>.</b>def<b><span style="color: blue;">$</span></b>/;</div>
<br />
Passes any characters followed or not by “abc” Eg. abcxyz12....<br />
<div class="code">
<span style="color: #0b5394;">var </span>G = /<span style="color: #cc0000;"><b>^</b></span>abc<b>.<span style="color: #38761d;">+</span><span style="color: #741b47;">?</span><span style="color: blue;">$</span></b>/</div>
<div class="code">
</div>
<div class="code">
</div>
<div class="code">
Stage 2</div>
<div class="code">
<br /></div>
<div class="code">
<b>Char</b> <b>Group Explanation</b><br />
<br />
[abc] Should match any single of character<br />
[^abc] Should not match any single character<br />
[a-zA-Z0-9] Characters range lowercase a-z, uppercase A-Z and numbers<br />
[a-z-._] Match against character range lowercase a-z and ._- special chats <br />
(.*?) Capture everything enclosed with brackets <br />
(com|info) Input should be “com” or “info”<br />
{2} Exactly two characters<br />
{2,3} Minimum 2 characters and Maximum 3 characters<br />
{2,} More than 2 characters<br />
<br />
<br />
Put together all in one URL validation.<br />
<div class="code">
<span style="color: #0b5394;">var</span> URL = /^(<span style="color: #e06666;">http</span>|<span style="color: #e06666;">https</span>|<span style="color: #e06666;">ftp</span>):\/\/(<span style="color: blue;">www</span>+\.)<b><span style="color: #741b47;">?</span></b>[a-zA-Z0-9]+\.([a-zA-Z]{2,4})\/<span style="color: #741b47;">?</span>/;<br />
<br />
URL.<span style="color: #cc0000;">test</span>(“<span style="color: #6fa8dc;">http://amitonrails.blogspot.in</span>”); <span style="color: orange;">// pass</span><br />
URL.<span style="color: #cc0000;">test</span>(“<span style="color: #6fa8dc;">http://www.</span><span style="color: #6fa8dc;">amitonrails.blogspot.in</span>”); <span style="color: orange;"> // pass</span><br />
URL.<span style="color: #cc0000;">test</span>(“<span style="color: #6fa8dc;">https://</span><span style="color: #6fa8dc;"><span style="color: #6fa8dc;">amitonrails.blogspot.in</span>/</span>”); <span style="color: orange;">// pass</span><br />
URL.<span style="color: #cc0000;">test</span>(“<span style="color: #6fa8dc;">http://</span><span style="color: #6fa8dc;">amitonrails.blogspot.in/2013/11/regular-expression-understanding.html</span>”); <span style="color: orange;">// pass</span></div>
<div class="code">
</div>
<div class="code">
</div>
<div class="code">
<div class="big">
Stage 3</div>
<br />
<b>Short Form</b> <b>Equivalent</b> <b>Explanation </b><br />
<br />
<b><span style="color: #cc0000;">\d</span> </b> [0-9] Any numbers<br />
<b><span style="color: blue;">\D</span> </b> [<span style="color: #cc0000;"><b>^</b></span>0-9] Any non-digits<br />
<span style="color: #b45f06;"><b>\w </b></span> [a-zA-Z0-9_] Characters,numbers and underscore<br />
<span style="color: #351c75;"><b>\W</b></span> [<span style="color: #cc0000;"><b>^</b></span>a-zA-Z0-9_] Except any characters, numbers and underscore<br />
<span style="color: #0b5394;"><b>\s </b></span> - White space character<br />
<b><span style="color: #274e13;">\S </span> </b> - Non white space character<br />
<br />
<br />
<div class="code">
<span style="color: #0b5394;">var</span> number = /<span style="color: #cc0000;">^</span>(\+<b><span style="color: #cc0000;">\d</span></b>{2,4})?<span style="color: #0b5394;"><b>\s</b></span>?(<span style="color: #cc0000;"><b>\d</b></span>{10})<span style="color: blue;"><b>$</b></span>/; <span style="color: orange;">// validating phone number</span><br />
<br />
number.<span style="color: #cc0000;">test</span>(1111111111); <span style="color: orange;">//pass</span><br />
number.<span style="color: #cc0000;">test</span>(+111111111111); <span style="color: orange;">//pass</span><br />
number.<span style="color: #cc0000;">test</span>(+11 1111111111); <span style="color: orange;">//pass</span><br />
number.<span style="color: #cc0000;">test</span>(11111111); <span style="color: orange;">//Fail </span></div>
</div>
<div class="code">
</div>
<div class="code">
</div>
<div class="code">
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-20985828656061274342013-10-13T23:56:00.002+05:302013-10-14T00:01:12.542+05:30Closures in Ruby<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Closures are <i>anonymous functions</i> with <i>closed scope</i>.
I'll dive into each of these concepts in more detail as I go, but first,
it's best to oversimplify. Think of a closure as a code block that you
can use as an argument, with special scoping rules. I'll use Ruby to
show how closures work. Listing 1 shows the simplest possible closure:
<br />
<br />
<a href="https://www.blogger.com/null" name="N100A7"><b>Listing 1. The simplest possible closure</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 50%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">3.times {puts "Inside the times method."}
Results:
Inside the times method.
Inside the times method.
Inside the times method.
</pre>
</td></tr>
</tbody></table>
<br />
<code>times</code> is a method on the <code>3</code> object. It executes the code in the closure three times. <code>{puts "Inside the times method."}</code> is the closure. It's an anonymous function that's passed into the <code>times</code> method and prints a static sentence. This code is tighter and simpler than the alternative with a <code>for</code> loop, shown in Listing 2:
<br />
<br />
<a href="https://www.blogger.com/null" name="N100C9"><b>Listing 2: Looping without closures</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">for i in 1..3
puts "Inside the times method."
end
</pre>
</td></tr>
</tbody></table>
<br />
The first extension that Ruby adds to the simple code block is an
argument list. A method or function can communicate with a closure by
passing in arguments. In Ruby, you represent the arguments with a
comma-separated list of arguments, between <code>||</code> characters, such as <code>|argument, list|</code>.
Using arguments in this way, you can easily build iteration into data
structures such as arrays. Listing 3 shows an example of iterating over
an array in Ruby:
<br />
<br />
<a href="https://www.blogger.com/null" name="N100DF"><b>Listing 3. Using closures with collections</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">['lions', 'tigers', 'bears'].each {|item| puts item}
Results:
lions
tigers
bears
</pre>
</td></tr>
</tbody></table>
<br />
The <code>each</code> method is just one way to iterate. Often, you want
to produce a new collection with the results of an operation. This
method in Ruby is called <code>collect</code>. You may also want to join
the contents of an array with some arbitrary string. Listing 4 shows
such an example. These are just two more of the many ways that you can
use closures to iterate.
<br />
<br />
<a href="https://www.blogger.com/null" name="N100F5"><b>Listing 4. Passing arguments to a closure</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">animals = ['lions', 'tigers', 'bears'].collect {|item| item.upcase}
puts animals.join(" and ") + " oh, my."
LIONS and TIGERS and BEARS oh, my.
</pre>
</td></tr>
</tbody></table>
<br />
In Listing 4, the first line of code takes each element of an array,
calls the closure on it, and then builds a collection with the results.
The second concatenates all of the elements into a sentence, with <code>" and "</code> between each one. So far, I've shown you nothing more than syntactic sugar. You can do all of these things in any language.
<br />
From the examples so far, you can see that an anonymous function is
simply a function, without an name, that is evaluated in place and
determines its context based on where it is defined. But if the only
difference between languages with closures and those without were a
little bit of syntactic sugar -- the fact that you don't need to declare
the function -- there wouldn't be much debate. The advantages of
closures go beyond saving a few lines of code, and the usage patterns go
beyond simple iteration.
<br />
The second part of the closure is the closed scope, which I can best
illustrate with another example. Given an array of prices, I'd like to
generate a sales-tax table with each price and its associated tax. I
don't want to hardcode the tax rate into the closure. I'd rather
configure it somewhere. Listing 5 shows one possible implementation:
<br />
<br />
<a href="https://www.blogger.com/null" name="N1010D"><b>Listing 5. Using closures to build a tax table</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">tax = 0.08
prices = [4.45, 6.34, 3.78]
tax_table = prices.collect {|price| {:price => price, :tax => price * tax}}
tax_table.collect {|item| puts "Price: #{item[:price]} Tax: #{item[:tax]}"}
Results:
Price: 4.45 Tax: 0.356
Price: 6.34 Tax: 0.5072
Price: 3.78 Tax: 0.3024
</pre>
</td></tr>
</tbody></table>
<br />
Before dealing with scoping, I should explain a couple of Ruby idioms. First, a <i>symbol</i> is an identifier preceded by a colon. Think of a symbol as a name for some abstract concept. <code>:price</code> and <code>:tax</code>
are symbols. Second, you can easily substitute variable values inside
strings. The sixth line takes advantage of this technique with <code>puts "Price: #{item[:price]} Tax: #{item[:tax]}"</code>. Now, back to the scoping issue.
<br />
Look at the first and fourth lines of code in Listing 5. The first assigns a value to the <code>tax</code>
variable. The fourth line uses that variable to compute the tax column
of the price table. But this usage is in a closure, so this code
actually executes in the context of the <code>collect</code> method! Now you have insight into the term <i>closure</i>.
The scope between the name space of the environment that defines the
code block and the function that uses it are essentially one scope: the
scope is closed. This characteristic is essential. This closed scope is
the communication that ties the closure to the calling function and the
code that defines it.
<br />
<div class="ibm-ind-link ibm-back-to-top">
<br /></div>
<a href="https://www.blogger.com/null" name="N10134"><span class="atitle">Customizing with closures</span></a><br />
You've seen how to use closures as a client. Ruby also lets you write
methods that use your own closures. This freedom means the Ruby API can
be more compact because Ruby doesn't need to define every usage model in
code. You can build in your own abstractions as needed, with closures.
For example, Ruby has a limited set of iterators, but the language gets
along fine without them because you can build your own iterative
concepts into your code through closures.
<br />
To build a function that uses a closure, you simply use the <code>yield</code> keyword to invoke the closure. Listing 6 shows an example. The <code>paragraph</code> function provides the first and last sentences of the output. The user can provide additional sentences with the closure.
<br />
<br />
<a href="https://www.blogger.com/null" name="N1014D"><b>Listing 6. Building methods that take closures</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">def paragraph
puts "A good paragraph should have a topic sentence."
yield
puts "This generic paragraph has a topic, body, and conclusion."
end
paragraph {puts "This is the body of the paragraph."}
Results:
A good paragraph should have a topic sentence.
This is the body of the paragraph.
This generic paragraph has a topic, body, and conclusion.</pre>
</td></tr>
</tbody></table>
<br />
<a href="https://www.blogger.com/null" name="N10154"><span class="smalltitle">Advantages</span></a><br />
You can easily take advantage of arguments within your custom closures by attaching a parameter list to <code>yield</code>, as in Listing 7:
<br />
<br />
<a href="https://www.blogger.com/null" name="N10165"><b>Listing 7. Attaching a parameter list</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">def paragraph
topic = "A good paragraph should have a topic sentence, a body, and a conclusion. "
conclusion = "This generic paragraph has all three parts."
puts topic
yield(topic, conclusion)
puts conclusion
end
t = ""
c = ""
paragraph do |topic, conclusion|
puts "This is the body of the paragraph. "
t = topic
c = conclusion
end
puts "The topic sentence was: '#{t}'"
puts "The conclusion was: '#{c}'"
</pre>
</td></tr>
</tbody></table>
<br />
Be careful to get the scoping right, though. The arguments that you
declare within the closure are local in scope. Listing 7, for example,
works, but Listing 8 would not because the <code>topic</code> and <code>conclusion</code> variables are both local in scope:<br />
<br />
<a href="https://www.blogger.com/null" name="N1017B"><b>Listing 8. Incorrect scoping</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">def paragraph
topic = "A good paragraph should have a topic sentence."
conclusion = "This generic paragraph has a topic, body, and conclusion."
puts topic
yield(topic, conclusion)
puts conclusion
end
my_topic = ""
my_conclusion = ""
paragraph do |topic, conclusion| # these are local in scope
puts "This is the body of the paragraph. "
my_topic = topic
my_conclusion = conclusion
end
puts "The topic sentence was: '#{topic}'"
puts "The conclusion was: '#{conclusion}'"
</pre>
</td></tr>
</tbody></table>
<br />
<div class="ibm-ind-link ibm-back-to-top">
<br /></div>
<a href="https://www.blogger.com/null" name="N10182"><span class="atitle">Closures in practice</span></a><br />
Some of the common closure scenarios are:
<br />
<ul>
<li>Refactoring</li>
<li>Customization</li>
<li>Iterating across collections</li>
<li>Managing resources</li>
<li>Enforcing policy</li>
</ul>
When you can build your own closures in a simple and convenient way, you
find techniques that open up a whole new range of possibilities.
Refactoring turns code that works into better code that works. Most Java
programmers look for refactoring possibilities <i>from the inside out</i>. They often look for repetition within the context of a method or loop. With closures, you can also refactor <i>from the outside in</i>.
<br />
Customization with closures can take you in some surprising places.
Listing 9, a quick example from Ruby on Rails, shows a closure that is
used to code the response of an HTTP request. Rails passes an incoming
request to a controller, which should generate the data the client
wants. (Technically, the controller renders the result based on the
contents the client sets in an HTTP <code>accept</code> header.) This concept is easy to communicate if you use a closure.
<br />
<br />
<a href="https://www.blogger.com/null" name="N101B2"><b>Listing 9. Rendering an HTTP result with closures</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">@person = Person.find(id)
respond_to do |wants|
wants.html { render :action => @show }
wants.xml { render :xml => @person.to_xml }
end
</pre>
</td></tr>
</tbody></table>
<br />
The code in Listing 9 is beautiful to look at, and with a quick glance,
you can tell exactly what it does. If the request block is requesting
HTML, it executes the first closure; if it's requesting XML, it executes
the second. You can also easily imagine the implementation. <code>wants</code> is an HTTP request wrapper. The code has two methods -- <code>xml</code> and <code>html</code> -- that each take closures. Each method can selectively call its closure based on the contents of the <code>accept</code> header, as in Listing 10:
<br />
<br />
<a href="https://www.blogger.com/null" name="N101D0"><b>Listing 10. Implementation of the request</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode"> def xml
yield if self.accept_header == "text/xml"
end
def html
yield if self.accept_header == "text/html"
end
</pre>
</td></tr>
</tbody></table>
<br />
Iteration is by far the most common use of closures within Ruby, but
this scenario is broader than using a collection's built-in closures.
Think of the types of collections you use every day. XML documents are
collections of elements. Web pages are a special case of XML. Databases
are made up of tables, which in turn are made up of rows. Files are
collections of characters or bytes, and often rows of text or objects.
Ruby solves each of these problems quite well within closures. You've
seen several examples of closures that iterate over collections. Listing
11 shows a closure that iterates over a database table:
<br />
<br />
<a href="https://www.blogger.com/null" name="N101DE"><b>Listing 11. Iterating through rows of a database</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">require 'mysql'
db=Mysql.new("localhost", "root", "password")
db.select_db("database")
result = db.query "select * from words"
result.each {|row| do_something_with_row}
db.close
</pre>
</td></tr>
</tbody></table>
<br />
The code in Listing 11 also shows another possible usage pattern. The
MySQL API forces users to set up the database resource and close it
using the <code>close</code> method. You could actually use a closure to
do the resource setup and cleanup instead. Ruby developers often use
this pattern to work with resources such as files. Using the Ruby API,
you don't need to open or close your file or manage the exceptions. The
methods on the <code>File</code> class handle that for you. Instead, you can use a closure, as in Listing 12:
<br />
<br />
<a href="https://www.blogger.com/null" name="N101F4"><b>Listing 12. Working with File using a closure</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">File.open(name) {|file| process_file(file)}
</pre>
</td></tr>
</tbody></table>
<br />
Closures have another huge advantage: they make it easy to enforce
policy. For example, if you have a transaction, you can make sure that
your code always brackets transactional code with the appropriate
function calls. Framework code can handle the policy, and user code --
provided in a closure -- can customize it. Listing 13 shows the basic
usage pattern:
<br />
<br />
<a href="https://www.blogger.com/null" name="N10202"><b>Listing 13. Enforcing policy</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">def do_transaction
begin
setup_transaction
yield
commit_transaction
rescue
roll_back_transaction
end
end
</pre>
</td></tr>
</tbody></table>
<br />
<div class="ibm-ind-link ibm-back-to-top">
</div>
<a href="https://www.blogger.com/null" name="N10209"><span class="atitle">Closures with the Java language</span></a><br />
The Java language does not formally support closures per se, but it does
allow the simulation of closures. Primarily, you can use anonymous
inner classes to implement closures. The Spring framework uses this
technique for many of the same reasons that Ruby does. Spring templates,
for persistence, allow iteration over result sets without burdening the
user with details of exception management, resource allocation, or
cleanup. Listing 14 shows an example from Spring's sample petclinic
application:<br />
<br />
<a href="https://www.blogger.com/null" name="N10217"><b>Listing 14. Inner classes as a closure substitute</b></a><br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;" summary="This table contains a code listing."><tbody>
<tr><td class="code-outline"><pre class="displaycode">JdbcTemplate template = new JdbcTemplate(dataSource);
final List names = new LinkedList();
template.query("SELECT id,name FROM types ORDER BY name",
new RowCallbackHandler() {
public void processRow(ResultSet rs)
throws SQLException
{
names.add(rs.getString(1));
}
});
</pre>
</td></tr>
</tbody></table>
<br />
Think of the things Listing 14's programmer doesn't need to do:
<br />
<ul>
<li>Open a connection</li>
<li>Close the connection</li>
<li>Handle iteration</li>
<li>Process exceptions</li>
<li>Deal with database-independent issues</li>
</ul>
The programmer is free from these issues because the framework handles
those problems. But anonymous inner classes give you only a loose
approximation of closures, and they don't go as far as you need to go.
Look at the wasted syntax in Listing 14. At least half of the example is
overhead. Anonymous classes are like a bucketful of cold water that's
spilled in your lap every time you want to use them. All of the extra
effort in wasted syntax discourages their use. Sooner or later, you're
going to quit. When language constructs are cumbersome and awkward, they
don't get used. The dearth of Java libraries that effectively use
anonymous inner classes make this problem manifest. For closures to be
practical and prevalent in the Java language, they must be quick and
clean.<br />
In the past, closures were never a serious priority for Java
developers. In the early years, the Java designers did not support
closures because Java users were skittish about automatically allocating
the variables on the heap without an explicit <code>new</code>.
Today, a tremendous debate circulates around including closures into the
base language. In recent years, practical interest in dynamic languages
such as Ruby, JavaScript, and even Lisp has led to a groundswell of
support for closures in the Java language. It looks like we'll finally
get closures in Java 1.7. Good things happen when you keep crossing
borders<br />
<br />
<span style="font-size: x-small;">source : <a href="http://www.ibm.com/developerworks/java/library/j-cb01097/index.html" target="_blank">http://www.ibm.com/developerworks</a></span></div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-60232328854423656072013-10-13T20:29:00.000+05:302013-10-13T20:29:14.434+05:30Make symbols with keyboard<div dir="ltr" style="text-align: left;" trbidi="on">
HOW TO MAKE SYMBOLS WITH KEYBOARD<br /> Alt + 0153..... ™... trademark symbol<br /> Alt + 0169.... ©.... copyright symbol<br /><span class="yiv2467184725text_exposed_show" id="yiv2467184725yui_3_7_2_17_1366551702189_83"> Alt + 0174..... ®....registered trademark symbol<br /> Alt + 0176 ...°......degre e symbol<br /> Alt + 0177 ...±....plus-or -minus sign<br /> Alt + 0182 ...¶.....paragr aph mark<br /> Alt + 0190 ...¾....fractio n, three-fourths<br /> Alt + 0215 ....×.....multi plication sign<br /> Alt + 0162...¢....the cent sign<br /> Alt + 0161.....¡..... .upside down exclamation point<br /> Alt + 0191.....¿..... upside down question mark<br /> Alt + 1.......☺....sm iley face<br /> Alt + 2 ......☻.....black smiley face<br /> Alt + 15.....☼.....sun<br /> Alt + 12......♀.....female sign<br /> Alt +
11.....♂......male sign<br /> Alt + 6.......♠.....spade<br /> Alt + 5.......♣...... Club<br /> Alt + 3.......♥...... Heart<br /> Alt + 4.......♦...... Diamond<br /> Alt + 13......♪.....eighth note<br /> Alt + 14......♫...... beamed eighth note<br /> Alt + 8721.... ∑.... N-ary summation (auto sum)<br /> Alt + 251.....√.....square root check mark<br /> Alt + 8236.....∞..... infinity<br /> Alt + 24.......↑..... up arrow<br /> Alt + 25......↓...... down arrow<br /> Alt + 26.....→.....right arrow<br /> Alt + 27......←.....left arrow<br /> Alt + 18.....↕......up/down arrow<br id="yiv2467184725yui_3_7_2_17_1366551702189_87" /> Alt + 29......↔...left right arrow</span></div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-16183411332965083982013-10-13T20:25:00.002+05:302013-10-13T20:25:46.952+05:30Windows Run Commands<div dir="ltr" style="text-align: left;" trbidi="on">
RUN Commands !<br /> <br /> 1. Accessibility Controls - access.cpl<br /> 2. Accessibility Wizard - accwiz<br /> 3. Add Hardware Wizard - hdwwiz.cpl<br /><span class="yiv9828742630text_exposed_show" id="yiv9828742630yui_3_7_2_27_1368031906162_82"> 4. Add/Remove Programs - appwiz.cpl<br /> 5. Administrative Tools - control admintools<br /> 6. Automatic Updates - wuaucpl.cpl<br /> 7. Bluetooth Transfer Wizard - fsquirt<br /> 8. Calculator - calc<br /> 9. Certificate Manager - certmgr.msc<br /> 10. Character Map - charmap<br /> 11. Check Disk Utility - chkdsk<br /> 12. Clipboard Viewer - clipbrd<br /> 13. Command Prompt - cmd<br /> 14. Component Services - dcomcnfg<br /> 15. Computer Management - compmgmt.msc<br /> 16. Control Panel - control<br /> 17. Date and Time Properties - timedate.cpl<br /> 18. DDE Shares - ddeshare<br /> 19. Device Manager -
devmgmt.msc<br /> 20. Direct X Troubleshooter - dxdiag<br /> 21. Disk Cleanup Utility - cleanmgr<br /> 22. Disk Defragment - dfrg.msc<br /> 23. Disk Management - diskmgmt.msc<br /> 24. Disk Partition Manager - diskpart<br /> 25. Display Properties - control desktop<br /> 26. Display Properties - desk.cpl<br /> 27. Dr. Watson System Troubleshooting Utility - drwtsn32<br /> 28. Driver Verifier Utility - verifier<br /> 29. Event Viewer - eventvwr.msc<br /> 30. Files and Settings Transfer Tool - migwiz<br /> 31. File Signature Verification Tool - sigverif<br /> 32. Findfast - findfast.cpl<br /> 33. Firefox - firefox<br /> 34. Folders Properties - control folders<br /> 35. Fonts - control fonts<br /> 36. Fonts Folder - fonts<br /> 37. Free Cell Card Game - freecell<br /> 38. Game Controllers - joy.cpl<br /> 39. Group Policy Editor (for xp professional) - gpedit.msc<br /> 40. Hearts Card Game - mshearts<br /> 41. Help and Support - helpctr<br /> 42. HyperTerminal - hypertrm<br /> 43. Iexpress Wizard -
iexpress<br /> 44. Indexing Service - ciadv.msc<br /> 45. Internet Connection Wizard - icwconn1<br /> 46. Internet Explorer - iexplore<br /> 47. Internet Properties - inetcpl.cpl<br /> 48. Keyboard Properties - control keyboard<br /> 49. Local Security Settings - secpol.msc<br /> 50. Local Users and Groups - lusrmgr.msc<br /> 51. Logs You Out Of Windows - logoff<br /> 52. Malicious Software Removal Tool - mrt<br /> 53. Microsoft Chat - winchat<br /> 54. Microsoft Movie Maker - moviemk<br /> 55. Microsoft Paint - mspaint<br /> 56. Microsoft Syncronization Tool - mobsync<br /> 57. Minesweeper Game - winmine<br /> 58. Mouse Properties - control mouse<br /> 59. Mouse Properties - main.cpl<br /> 60. Netmeeting - conf<br /> 61. Network Connections - control netconnections<br /> 62. Network Connections - ncpa.cpl<br /> 63. Network Setup Wizard - netsetup.cpl<br /> 64. Notepad - notepad<br /> 65. Object Packager - packager<br /> 66. ODBC Data Source Administrator - odbccp32.cpl<br /> 67. On Screen Keyboard -
osk<br /> 68. Outlook Express - msimn<br /> 69. Paint - pbrush<br /> 70. Password Properties - password.cpl<br /> 71. Performance Monitor - perfmon.msc<br /> 72. Performance Monitor - perfmon<br /> 73. Phone and Modem Options - telephon.cpl<br /> 74. Phone Dialer - dialer<br /> 75. Pinball Game - pinball<br /> 76. Power Configuration - powercfg.cpl<br /> 77. Printers and Faxes - control printers<br /> 78. Printers Folder - printers<br /> 79. Regional Settings - intl.cpl<br /> 80. Registry Editor - regedit<br /> 81. Registry Editor - regedit32<br /> 82. Remote Access Phonebook - rasphone<br /> 83. Remote Desktop - mstsc<br /> 84. Removable Storage - ntmsmgr.msc<br /> 85. Removable Storage Operator Requests - ntmsoprq.msc<br /> 86. Resultant Set of Policy (for xp professional) - rsop.msc<br /> 87. Scanners and Cameras - sticpl.cpl<br /> 88. Scheduled Tasks - control schedtasks<br /> 89. Security Center - wscui.cpl<br /> 90. Services - services.msc<br /> 91. Shared Folders - fsmgmt.msc<br /> 92. Shuts
Down Windows - shutdown<br /> 93. Sounds and Audio - mmsys.cpl<br /> 94. Spider Solitare Card Game - spider<br /> 95. SQL Client Configuration - cliconfg<br /> 96. System Configuration Editor - sysedit<br /> 97. System Configuration Utility - msconfig<br /> 98. System Information - msinfo32<br /> 99. System Properties - sysdm.cpl<br /> 100. Task Manager - taskmgr<br /> 101. TCP Tester - tcptest<br /> 102. Telnet Client - telnet<br /> 103. User Account Management - nusrmgr.cpl<br /> 104. Utility Manager - utilman<br /> 105. Windows Address Book - wab<br /> 106. Windows Address Book Import Utility - wabmig<br /> 107. Windows Explorer - explorer<br /> 108. Windows Firewall - firewall.cpl<br /> 109. Windows Magnifier - magnify<br /> 110. Windows Management Infrastructure - wmimgmt.msc<br /> 111. Windows Media Player - wmplayer<br /> 112. Windows Messenger - msmsgs<br /> 113. Windows System Security Tool - syskey<br /> 114. Windows Update Launches - wupdmgr<br /> 115. Windows Version - winver<br /> 116.
Wordpad - write</span></div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0tag:blogger.com,1999:blog-2186340835215550032.post-59769571184534573952013-10-10T14:24:00.001+05:302013-10-10T15:00:51.753+05:30MySql Database Replication<div dir="ltr" style="text-align: left;" trbidi="on">
Below is the step by step procedure to create a database replication in MySql. <br />
<br />
1 Configure The Master<br />
First we have to edit /etc/mysql/my.cnf. or /etc/my.cnf We have to
enable networking for MySQL, and MySQL should listen on all IP
addresses, therefore we comment out these lines (if existant):<br />
#skip-networking<br />
skip-external-locking<br />
bind-address=0.0.0.0<br />
log-bin=mysql-bin.log<br />
binlog-do-db=exampledb (database name)<br />
server-id=1<br />
<br />
Then we restart MySQL:<br />
/etc/init.d/mysql restart<br />
<br />
<br />
Create a user with replication privileges<br />
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY
'<some_password>'; (Replace <some_password> with a real
password!) <br />
FLUSH PRIVILEGES;<br />
<br />
Take dump of database(exampledb) and run command<br />
SHOW MASTER STATUS<br />
It will give you result like<br />
---------------+----------+---<br />
<div dir="ltr">
<wbr></wbr>-----------+------------------<wbr></wbr>+<br />
| File | Position | Binlog_do_db | Binlog_ignore_db |<br />
+---------------+----------+--<wbr></wbr>------------+-----------------<wbr></wbr>-+<br />
| mysql-bin.006 | 183 | database name| |<br />
+---------------+----------+--<wbr></wbr>------------+-----------------<wbr></wbr>-+<br />
1 row in set (0.00 sec<br />
<br />
<br />
Write down this information, we will need it later on the slave!<br />
Then leave the MySQL shell:<br />
quit;<br />
<br />
2 Configure The Slave<br />
On the slave we first have to create the sample database exampledb:<br />
mysql -u root -p<br />
Enter password:<br />
CREATE DATABASE exampledb;<br />
quit;<br />
<br />
store databse dump on slave<br />
<br />
Now we have to tell MySQL on the slave that it is the slave, that the
master is 192.168.0.100, and that the master database to watch is
exampledb. Therefore we add the following lines to /etc/mysql/my.cnf or
/etc/my.cnf if file doesnot exists copy from other location<br />
<br />
server-id=2<br />
master-host=192.168.0.100(ip address of master host machine)<br />
master-user=slave_user(user name)<br />
master-password=secret (password)<br />
master-connect-retry=60<br />
replicate-do-db=exampledb (database name)<br />
<br />
Then we restart MySQL:<br />
/etc/init.d/mysql restart<br />
<br />
Finally, we must do this:<br />
mysql -u root -p<br />
Enter password:<br />
SLAVE STOP;<br />
In the next command (still on the MySQL shell) you have to replace the values appropriately:<br />
CHANGE MASTER TO MASTER_HOST=’master ip address’,
MASTER_USER='slave_user', MASTER_PASSWORD='<some_<wbr></wbr>password>',
MASTER_LOG_FILE='mysql-bin.<wbr></wbr>006', MASTER_LOG_POS=183;<br />
• MASTER_HOST is the IP address or hostname of the master (in this example it is 192.168.0.100).<br />
• MASTER_USER is the user we granted replication privileges on the master.<br />
• MASTER_PASSWORD is the password of MASTER_USER on the master.<br />
• MASTER_LOG_FILE is the file MySQL gave back when you ran SHOW MASTER STATUS; on the master.<br />
• MASTER_LOG_POS is the position MySQL gave back when you ran SHOW MASTER STATUS; on the master.<br />
Now all that is left to do is start the slave. Still on the MySQL shell we run<br />
START SLAVE;<br />
quit;<br />
That's it! Now whenever exampledb is updated on the master, all changes will be replicated to exampledb on the slave. Test it!</div>
<div dir="ltr">
</div>
<div dir="ltr">
</div>
<div dir="ltr">
If you have any queries or didnt understood any step feel free to ask.</div>
</div>
Anonymoushttp://www.blogger.com/profile/09573794257346095080noreply@blogger.com0