<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>programming &#8211; redpanthers.co</title>
	<atom:link href="/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Fri, 12 Aug 2016 08:49:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>Lazy enumerator to handle huge files</title>
		<link>/lazy-enumerator-to-handle-huge-files/</link>
				<pubDate>Fri, 12 Aug 2016 08:49:12 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[enumerator]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[huge files]]></category>
		<category><![CDATA[lazy]]></category>
		<category><![CDATA[mangaing]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=401</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[<b>Lazy evaluation</b>, or <b>call-by-need</b> is an evaluation strategy which delays the evaluation of an expression until its value is needed. It&#8217;s frequently seen in functional languages, ruby introduced the lazy method in Ruby 2.0. For those who don&#8217;t know what are enumerators: enumerators are something that can be counted. So a collection of elements, files (file is an collection of lines of string), etc can be treated as an enumerator.
In ruby we need to make something countable into an enumerator object, which is done by applying .each and .map on it.


<pre class="lang:ruby decode:true">[].each
[1, 3, 4].each
file.each
{}.each</pre>


Ruby has a wide range of operations we can do over a collection, it&#8217;s one of those features that makes Ruby such a powerful dynamic language. An enumerator can be used to generate series like the Fibonacci series.


<pre class="lang:ruby decode:true">fib = Enumerator.new do |y|
  a = b = 1
  loop do
    y &lt;&lt; a
    a, b = b, a + b
  end
end</pre>


But when we do a <em>.map</em> / <em>.each</em> with a code block, then it would try to realize the enumerator fully and then apply the block over it.
<i></i>That would be fine when we are working on something small like:


<pre class="lang:default decode:true">[1,2,3].map { |i| i*i }</pre>


But when we take the above fib enumerator, which will grow into an infinite series, adding a <em>.map </em>would lead the code to an infinite loop. If you are crazy enough to write an infinite loop, feel free to run the below code.


<pre class="lang:ruby decode:true">foo.map { |i| i * * }</pre>


When we try to use these operations on a huge 10 GB file,there it makes your program realize the entire file is close to impossible. So this is the case where the lazy enumerator comes to place. It would ask ruby to defer the realization of the enumerator until it is required, and not before you run your operation.
So with respect to the above Fibonacci enumerator, you should change the code to as below.


<pre class="lang:ruby decode:true">foo.lazy.map { |i| i * i }</pre>


So now, the program is ready, all good it would run fast. But it won&#8217;t execute them until you call or access those elements. So only when we do <code>foo.lazy.map { |i| i* i }.</code> First the first element is calculated and given to us. Like wise if you want the first 10 elements of the fibonacci series then you have to do <code>foo.lazy.map { |i| i* i }.take(1o).to_a</code>
To do the  same with the files, as a file is a collection of lines, follow the below code snippets:


<pre class="lang:ruby decode:true">fille = File.open('OurHugeFile.log')
</pre>


<span style="line-height: 1.5;">get the first three lines, lazily:</span>


<pre class="lang:ruby decode:true">file.each_line.lazy.take(3).to_a</pre>


get the firs two lines with word &#8220;INFO&#8221;


<pre class="lang:ruby decode:true "> file.each_line.lazy.select { |line| line.match(/INFO/) }.first(2)
</pre>


So if you want to print something with line numbers, just do a map from the lazy.


<pre class="">file.each_line.lazy.each_with_index.map { |l, i| "#{i}: #{l}" }.select { |i| match(/INFO/) }.first(3)</pre>




<hr />
If you have anything more to add to this, kindly comment below.]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Working with timezones in rails</title>
		<link>/working-timezones-rails/</link>
				<pubDate>Thu, 11 Aug 2016 07:32:15 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[different]]></category>
		<category><![CDATA[handelling]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[timzones]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=386</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[<strong>Ruby on Rails</strong> being an amazing framework, helps us manage the timezone of our rails application. It gives us access to a lot of helpers, to make our life easier. For example, if you want to change all the date and time of your application to the logged in users time zone, we just have to place the following code in the application_controller.


<pre class="lang:ruby decode:true "># app/controllers/application_controller.rb
around_action :set_time_zone, if: :current_user
private
def set_time_zone(&amp;block)
  Time.use_zone(current_user.time_zone)
end</pre>


We assume that you have stored the user&#8217;s time_zone in your database in the time_zone column.
The application  to show  timezone can be set in your <code>application.rb</code>, if we don&#8217;t set a particular timezone then the application will just show the systems timezone.


<pre class="lang:default decode:true">config.time_zone = 'Central Time (US &amp; Canada)'</pre>


If you want to know all the timezone options available in rails, run the <code>rake -D time</code> command in your terminal.
Even though rails would take care of the timezone, when we are using certain ruby commands, it gives us our systems timezone and not the one set by rails. So to avoid surprises, we should be aware of the timezones we are exposed to.
A rails app, would always be exposed to three timezones:


<ul>
 	

<li>System timezone</li>


 	

<li>Database timezone</li>


 	

<li>Rails applications own timezone</li>


</ul>


All three could be different, for example <em>your system timezone could be in IST, database in UTC, and rails app running in PDT</em>.
To avoid accidental  assessment to a different timezone, always keep in mind the commands you should and should not use.
DO NOT USE &#8211; as they all give you time with respect to the system time


<pre class="highlight"><code>  * Time.now
  * Date.today
  * Date.today.to_time
  * Time.parse("2015-07-04 17:05:37")
  * Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")</code></pre>


USE &#8211; these give you access to the rails application time (which we can change with respect to the user)


<pre class="highlight"><code>* Time.current
* 2.hours.ago
* Time.zone.today
* Date.current
* 1.day.from_now
* Time.zone.parse("2015-07-04 17:05:37")
* Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone</code></pre>


&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
	</channel>
</rss>
