<?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>A Waage Blog</title>
	<atom:link href="http://qugstart.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://qugstart.com/blog</link>
	<description>Ruby, Rails, Life</description>
	<lastBuildDate>Thu, 19 Apr 2012 06:06:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails testing with Machinist 2, Rspec, Database Cleaner Gem</title>
		<link>http://qugstart.com/blog/ruby-and-rails/rails-testing-with-machinist-2-rspec-database-cleaner-gem/</link>
		<comments>http://qugstart.com/blog/ruby-and-rails/rails-testing-with-machinist-2-rspec-database-cleaner-gem/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 01:59:54 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Rails Testing]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[database_cleaner]]></category>
		<category><![CDATA[machinist2]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=201</guid>
		<description><![CDATA[QUICK vent and advice when using Machinst2 and Database Cleaner to test in Rails:
 TURN OFF MACHINIST CACHING! 
Add this to your environments/test.rb file:

Machinist.configure do &#124;config&#124;
  config.cache_objects = false
end

Machinist tries to do some weird caching to make your tests run faster. But, it doesn&#8217;t quite work the way you&#8217;d expect. If you are running [...]]]></description>
			<content:encoded><![CDATA[<p>QUICK vent and <strong>advice</strong> when using <a href="https://github.com/notahat/machinist">Machinst2</a> and <a href="https://github.com/bmabey/database_cleaner">Database Cleaner</a> to test in Rails:</p>
<h2> TURN OFF MACHINIST CACHING! </h2>
<p>Add this to your environments/test.rb file:</p>
<pre class='prettyprint' lang='ruby'>
Machinist.configure do |config|
  config.cache_objects = false
end
</pre>
<p>Machinist tries to do some weird caching to make your tests run faster. But, it doesn&#8217;t quite work the way you&#8217;d expect. If you are running into strange problems where your objects are persisting through many tests, even though you are using DatabaseCleaner after each test, you might try this. If you run into problems where running one test at a time works, but running &#8220;rake spec&#8221; results in errors, this is also worth a shot. <em>Don&#8217;t let Machinist caching drive you nuts!</em> <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Sidenote: In my experience, the best way to debug these errors that appear when running the entire test suite, but do not appear when running individual tests is to use rspec to run all but one test. Remove one at a time, and see if removing that single test helps eliminate errors.<br />
Example:</p>
<pre class='prettyprint' lang='bash'>
# If this gives errors:
$ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the first
 $ bundle exec rspec ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the 2nd
 $ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/favorite_spec.rb
# Repeat...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/ruby-and-rails/rails-testing-with-machinist-2-rspec-database-cleaner-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thinking Big (Data)</title>
		<link>http://qugstart.com/blog/big-data/thinking-big-data/</link>
		<comments>http://qugstart.com/blog/big-data/thinking-big-data/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 05:49:35 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Big Data]]></category>
		<category><![CDATA[Data mining]]></category>
		<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Rackspace Cloud]]></category>
		<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[rackspace]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=190</guid>
		<description><![CDATA[Recently I&#8217;ve been doing lots of &#8220;big data&#8221; sorta things. As a result, I&#8217;ve been forced to look at solving programming problems from a slightly new perspective.
Doing things like data mining with huge dataset pre-processing and manipulation, matrix computation, and other machine learning tasks can be computationally expensive and take forever! This has forced me [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been doing lots of &#8220;big data&#8221; sorta things. As a result, I&#8217;ve been forced to look at solving programming problems from a slightly new perspective.</p>
<p>Doing things like data mining with huge dataset pre-processing and manipulation, matrix computation, and other machine learning tasks can be computationally expensive and take forever! This has forced me to pay close attention to things such as code efficiency (not using Ruby for certain things &#8211; gasp!), database tuning, refactoring queries, optimal database schemas (and database choices), caching at all levels (application, db, etc), parallel processing (along with all its problems, concurrency issues, deadlocks, etc), system memory management, map-reduce, and on, and on..</p>
<p>One thing I especially love about working with massive data sets is that it really forces me to think &#8220;big&#8221;. No longer am I constrained to solving the problem with a single machine! In a couple clicks, I can create additional servers or double or triple the size of my server, quickly do computation or processing, and then bring the server back down to a more affordable size. </p>
<p>In this day and age, with cloud computing services like <a href="http://aws.amazon.com/ec2/">Amazon EC2</a> / <a href="http://www.rackspace.com/cloud/cloud_hosting_products/servers/">Rackspace</a> at your fingertips, you only pay for what you use. This is an important concept to consider, and definitely requires a different way of thinking about solving a &#8220;big&#8221; problem. </p>
<p>Of course the first step should still be to optimize your code, and use the right tools for the right problem. But, these are additional tools in your toolbox, that really pay off when you need them.</p>
<p>Here&#8217;s an example I recently encountered.</p>
<p>Prototype:<br />
1. I started with a small test dataset matrix (3 MB, 3000 rows)<br />
2. I wrote a Ruby/mysql script that would prepare this matrix from our DB. It took about 5 minutes, and that was fine. 5 minutes I can spare.<br />
3. Next, I wanted to try this on my 2nd dataset matrix (20MB, 20000 rows). Hmm.. took over half an hour. That&#8217;s not THAT bad. But, imagine running this on a 200MB dataset, 20 GB dataset, or 2 TB dataset ?!! Gotta do better.</p>
<p>Step 1: optimize code / queries:<br />
I spent a good amount of time tuning Mysql&#8217;s memory/caching/threads settings, making smarter queries, doing joins and de-normalizing tables to reduce queries. Knowing that Ruby is not the fastest language out there <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  &#8211; I tried to be smarter about not doing extra iterations, using extra memory, etc.<br />
<strong>- Result:</strong> I was able to cut down the time of the 2nd dataset from 30 minutes to about <strong>7 minutes</strong>! (not bad)</p>
<p>Step 2: Parallelize code<br />
I knew that the code was basically iterating over a huge array, so I wanted to try parallizing some of the actions it was performing. This was my first try at using Ruby&#8217;s <a href="https://github.com/grosser/parallel">Parallel gem</a>, which allows you to split up any code into parallel threads or processes. Pretty cool stuff. I experimented with a bunch of different #&#8217;s of processes / threads to find out what the best settings were. Turns out the optimal setting was to use 4 concurrent processes. For what I was doing, my code was accessing the database (very costly I/O operations). So, the database was actually a bottleneck. Using more than 4 concurrent processes would result in too much DB traffic, and would hurt the performance.<br />
<strong>-Result:</strong> I was able to cut down the time of the 2nd dataset from 7 minutes to about <strong>1.5 minutes</strong>! (Getting there!)</p>
<p>Step 3: Try my sexy parallelized code on a bigger data-set (12GB).<br />
- Of course I hit all kinds of problems &#8211; memory limit problems, database collisions, what&#8217;s the optimum number of parallel processes? etc, etc. Not so fast!<br />
<strong>-Result:</strong> I was forced to run the code on a single process, that took something like <strong>5-6 hours</strong> (boo <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> )</p>
<p>Step 4: Figure out how to run the parallelized code on the large dataset<br />
This required examining the logs and figuring out why I was hitting bottlenecks. It turns out that when I ran many concurrent processes, they were all fighting for DB access. Mysql didn&#8217;t like this and in the end, all processes became stalled. I found that by putting an increasing delay in each subsequent child process, I could avoid collisions. Since each process performed a huge DB query up front, and then spent the rest of its life using memory / CPU (but not competing for DB), they would all run together, nicely.<br />
<strong>-Result:</strong>I was able to cut down the time of the huge dataset from 5-6 hours to about <strong>1.5 hour</strong>! (yes!)</p>
<p>Step 5: Not really completely satisfied, I wanted to try enlarging my cloud instance and running the same exact command. I knew that on my 4GB server, the CPU and memory were all maxed out by my processing script. So, I tried increasing it to 16GB. The results were not so stellar.<br />
<strong>-Result:</strong> I was able to cut down the time of the huge dataset from 1.5 hours on the 4GB machine to about <strong>1 hour</strong> on the 16GB machine. This is with no additional db tuning (to make use of the additional system resources), nor adjusting the number of processes (a powerful server could have made use of more concurrent processes).</p>
<p><strong>What I should have done next: </strong>Tune the db settings, tune the number of processes, and try again.</p>
<p><strong>What I actually did instead:</strong> Called it a day, and wrote this article <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>My personal takeaways:<br />
- Big data is exciting<br />
- Use the right tools for the right job<br />
- Optimize my code<br />
- Tune my DB settings<br />
- Use the cloud (Thanks <a href="http://www.rackspace.com/cloud/cloud_hosting_products/servers/">Rackspace</a>!)<br />
- Think big!</p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/big-data/thinking-big-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Comodo PositiveSSL Certificate with Node.js</title>
		<link>http://qugstart.com/blog/node-js/install-comodo-positivessl-certificate-with-node-js/</link>
		<comments>http://qugstart.com/blog/node-js/install-comodo-positivessl-certificate-with-node-js/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 00:42:02 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Comodo]]></category>
		<category><![CDATA[Express.js]]></category>
		<category><![CDATA[PositiveSSL]]></category>
		<category><![CDATA[SSL Certificate]]></category>
		<category><![CDATA[SSLCertificateChainFile]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=185</guid>
		<description><![CDATA[So today I tried to install the cheap Comodo PositiveSSL certificate to use on my Node.js / express.js server. Unfortunately, all the documentation and examples of installing an SSL certificate on a Node.js server only mention two options in the createServer() method (See my full example here) : 

var https = require('https');
var fs = require("fs");

var [...]]]></description>
			<content:encoded><![CDATA[<p>So today I tried to install the cheap <a href="http://www.positivessl.com/">Comodo PositiveSSL certificate</a> to use on my Node.js / express.js server. Unfortunately, all the documentation and examples of installing an SSL certificate on a Node.js server only mention two options in the createServer() method (<a href="http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/">See my full example here</a>) : </p>
<pre class='prettyprint' lang='javascript'>
var https = require('https');
var fs = require("fs");

var https_options = {
  key: fs.readFileSync("/path/to/server.key"),
  cert: fs.readFileSync("/path/to/mydomain.crt")
};
var https_server = https.createServer(https_options);
</pre>
<p>However, with the PositiveSSL certificate, Comodo will actually send you 3 files:<br />
1) PositiveSSLCA2.crt<br />
2) AddTrustExternalCARoot.crt<br />
3) mydomain.crt</p>
<p>This is quite confusing for someone who doesn&#8217;t really understand (nor want to understand) all the details of how an SSL certificate works. Which one do I use for the cert: option?? </p>
<p>Naturally, I started with the mydomain.crt file. This led to a cryptic web browser error message:<br />
&#8220;this certificate was signed by an unknown authority&#8221;</p>
<p>So, a bit of googling found that when installing the PositiveSSL cert on Apache servers, you must use a chain file (mod_ssl option: SSLCertificateChainFile). If you check the <a href="http://httpd.apache.org/docs/2.0/mod/mod_ssl.html">Apache mod_ssl documentation</a> you will see that this file is a concatenation of certificate files:</p>
<p>&#8220;Such a file is simply the concatenation of the various PEM-encoded CA Certificate files, usually in certificate chain order.&#8221;</p>
<p>So, what you have to do is the following:<br />
1) Create a &#8220;bundle&#8221; file by concatenating the PositiveSSLCA2 and AddTrustExternalCARoot certificates</p>
<pre lang='bash' class='prettyprint'>
cat PositiveSSLCA2.crt AddTrustExternalCARoot.crt > mydomain.ca-bundle
</pre>
<p>2) Add this certificate as the &#8220;ca&#8221; option when creating your Node.js sever:</p>
<pre lang='javascript' class='prettyprint'>
var https = require('https');
var fs = require("fs");

var https_options = {
  ca: fs.readFileSync("/path/to/mydomain.ca-bundle"),
  key: fs.readFileSync("/path/to/server.key"),
  cert: fs.readFileSync("/path/to/mydomain.crt")
};
var https_server = https.createServer(https_options);
</pre>
<p>This should properly set up the CA chain so that browsers can verify the SSL certificate.</p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/node-js/install-comodo-positivessl-certificate-with-node-js/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quick! create a self-signed SSL certificate in Ubuntu</title>
		<link>http://qugstart.com/blog/linux/quickest-way-to-create-a-self-signed-ssl-certificate-in-ubuntu/</link>
		<comments>http://qugstart.com/blog/linux/quickest-way-to-create-a-self-signed-ssl-certificate-in-ubuntu/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 23:20:51 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=180</guid>
		<description><![CDATA[Especially for testing SSL, I can never remember how to create a self-signed certificate. Here&#8217;s a quick and dirty guide to setting up a self-signed certificate. Obviously, not recommended for production setups  
We will create the certificate in the &#8220;/etc/ssl/self-signed&#8221; directory

# Become root first !
$ mkdir /etc/ssl/self-signed &#038;&#038; cd /etc/ssl/self-signed
$ openssl genrsa -des3 -out [...]]]></description>
			<content:encoded><![CDATA[<p>Especially for testing SSL, I can never remember how to create a self-signed certificate. Here&#8217;s a quick and dirty guide to setting up a self-signed certificate. Obviously, not recommended for production setups <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>We will create the certificate in the &#8220;/etc/ssl/self-signed&#8221; directory</p>
<pre lang='bash' class='prettyprint'>
# Become root first !
$ mkdir /etc/ssl/self-signed &#038;&#038; cd /etc/ssl/self-signed
$ openssl genrsa -des3 -out server.key 1024
$ openssl rsa -in server.key -out server.key.insecure
$ mv server.key server.key.secure &#038;&#038; mv server.key.insecure server.key
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
</pre>
<p>Now, you can use the files server.key and server.crt in your web server setups. See my post on <a href="http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/">Setting up Node.js with to accept HTTP and HTTPS connections</a> for a quick example of using this SSL certificate with Node.js!</p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/linux/quickest-way-to-create-a-self-signed-ssl-certificate-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Node.js and RESTify Server with both HTTP and HTTPS</title>
		<link>http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/</link>
		<comments>http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 23:05:43 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=175</guid>
		<description><![CDATA[Here&#8217;s a quick guide on setting up a Node.js server using the restify module that accepts BOTH http and https connections.
We want to re-use as much code as possible because the two servers (HTTP and HTTPS) are identical besides the protocol.
In the example, we created a self-signed SSL certificate. If you need help on that, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick guide on setting up a Node.js server using the <a href="http://mcavage.github.com/node-restify/">restify module</a> that accepts BOTH http and https connections.</p>
<p>We want to re-use as much code as possible because the two servers (HTTP and HTTPS) are identical besides the protocol.</p>
<p>In the example, we created a self-signed SSL certificate. If you need help on that, check out this <a href="http://qugstart.com/blog/linux/quickest-way-to-create-a-self-signed-ssl-certificate-in-ubuntu/">quick guide on how to set up a self-signed SSL certificate on Ubuntu</a>.</p>
<pre class='prettyprint' lang='javascript'>
var restify = require('restify');
var fs = require('fs');

// Setup some https server options
var https_options = {
  key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
  certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

// Instantiate our two servers
var server = restify.createServer();
var https_server = restify.createServer(https_options);

// Put any routing, response, etc. logic here. This allows us to define these functions
// only once, and it will be re-used on both the HTTP and HTTPs servers
var setup_server = function(app) {
        function respond(req, res, next) {
           res.send('I see you ' + req.params.name);
        }

        // Routes
        app.get('/test/:name', respond);
}

// Now, setup both servers in one step
setup_server(server);
setup_server(https_server);

// Start our servers to listen on the appropriate ports
server.listen(80, function() {
  console.log('%s listening at %s', server.name, server.url);
});

https_server.listen(443, function() {
  console.log('%s listening at %s', https_server.name, https_server.url);
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to install Node.js on Ubuntu &#8211; in under 5 minutes</title>
		<link>http://qugstart.com/blog/node-js/how-to-install-node-js-on-ubuntu-and-create-an-app-in-5-minutes/</link>
		<comments>http://qugstart.com/blog/node-js/how-to-install-node-js-on-ubuntu-and-create-an-app-in-5-minutes/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 05:35:53 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[express]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=162</guid>
		<description><![CDATA[Follow along, as we create our first Node.js app using the &#8220;express&#8221; framework in under 5 minutes!
Note: We&#8217;re on Ubuntu 10.04 LTS (as root!)
# Update the system and install some necessary packages

$ apt-get update
$ apt-get install g++ curl libssl-dev apache2-utils
$ apt-get install git-core

# Find the latest package to install at nodejs.org (example uses 0.6.11)

$ wget [...]]]></description>
			<content:encoded><![CDATA[<p>Follow along, as we create our first Node.js app using the &#8220;express&#8221; framework in under 5 minutes!<br />
Note: We&#8217;re on Ubuntu 10.04 LTS (as root!)</p>
<p># Update the system and install some necessary packages</p>
<pre lang='bash' class='prettyprint'>
$ apt-get update
$ apt-get install g++ curl libssl-dev apache2-utils
$ apt-get install git-core
</pre>
<p># Find the latest package to install at nodejs.org (example uses 0.6.11)</p>
<pre lang='bash' class='prettyprint'>
$ wget http://nodejs.org/dist/v0.6.11/node-v0.6.11.tar.gz
$ tar xzf node-v0.6.11.tar.gz
$ cd node-v0.6.11
$ ./configure
$ make
$ make install
</pre>
<p># Install npm (Node.js package manager)</p>
<pre lang='bash' class='prettyprint'>
$ cd /usr/local/src
$ git clone http://github.com/isaacs/npm.git
$ cd npm/
$ make install
</pre>
<p># Install express framework, -g flag installs the express executable globally</p>
<pre lang='bash' class='prettyprint'>
$ npm install -g express
</pre>
<p># Create an app with the express framework</p>
<pre lang='bash' class='prettyprint'>
$ express /var/www/foo
$ cd /var/www/foo/
</pre>
<p># Install dependencies</p>
<pre lang='bash' class='prettyprint'>
$ npm install -d
</pre>
<p># Start the node app!</p>
<pre lang='bash' class='prettyprint'>
$ node app.js
</pre>
<p>Voila! I told you it was fast <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/node-js/how-to-install-node-js-on-ubuntu-and-create-an-app-in-5-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSV Escaping Double Quotes</title>
		<link>http://qugstart.com/blog/linux/csv-escaping-double-quotes/</link>
		<comments>http://qugstart.com/blog/linux/csv-escaping-double-quotes/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 07:51:49 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[1-liner]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[csv format]]></category>
		<category><![CDATA[double-quotes]]></category>
		<category><![CDATA[escaping]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[fastercsv]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=159</guid>
		<description><![CDATA[What is the proper way to escape double-quotes in CSV files?
When you try to export from MySQL in CSV format, the MySQL reference manual itself suggests using 

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

Now, I thought I had a nicely formatted CSV file where fields that have double quotes would be properly escaped. Looking [...]]]></description>
			<content:encoded><![CDATA[<p>What is the proper way to escape double-quotes in CSV files?</p>
<p>When you try to export from MySQL in CSV format, the <a href="http://dev.mysql.com/doc/refman/5.0/en/select.html">MySQL reference manual</a> itself suggests using </p>
<pre lang="mysql" class="prettyprint">
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
</pre>
<p>Now, I thought I had a nicely formatted CSV file where fields that have double quotes would be properly escaped. Looking through this .csv file, I can see that sure enough, all the double-quotes are turned from &#8221; into \&#8221;.</p>
<p>This is fine, except that when I tried to use Excel to open the CSV file or <a href="http://fastercsv.rubyforge.org/">FasterCSV</a> (a nice Ruby library for working with CSV files), I find that the double quotes were not escaped &#8220;properly&#8221;. They are expecting to escape double-quotes with TWO double-quotes (&#8221;"). According to <a href="http://www.csvreader.com/csv_format.php">CSVreader.com</a>, this is a known discrepancy between the way different programs escape double-quotes in CSV files. Great.</p>
<p>Well, don&#8217;t worry &#8211; here&#8217;s a sed 1-liner that you can use on the command line to convert those \&#8221; (backslash double-quote) into &#8220;&#8221; (two double-quotes):</p>
<pre lang="bash" class="prettyprint">
$cat file.csv | sed -E 's/\\"/""/g' > file2.csv
</pre>
<p>Hope that helps someone out there!</p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/linux/csv-escaping-double-quotes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby floats, BigDecimals and money (currency)</title>
		<link>http://qugstart.com/blog/ruby-and-rails/ruby-floats-bigdecimals-and-money-currency/</link>
		<comments>http://qugstart.com/blog/ruby-and-rails/ruby-floats-bigdecimals-and-money-currency/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 00:29:22 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Rails Testing]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[BigDecimals]]></category>
		<category><![CDATA[currency]]></category>
		<category><![CDATA[Floats]]></category>
		<category><![CDATA[money]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=151</guid>
		<description><![CDATA[Fellow Ruby-ers, please be warned!!! DO NOT use Ruby floats when performing arithmetic calculations involving money!
My calculations work in IRB, so I was really confused when I ran into this weird situation where (what I thought was) a simple arithmetic calculation led to strange results in my unit tests (I cannot stress the importance of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Fellow Ruby-ers, please be warned!!! DO NOT use Ruby floats when performing arithmetic calculations involving money!</strong></p>
<p>My calculations work in IRB, so I was really confused when I ran into this weird situation where (what I thought was) a simple arithmetic calculation led to strange results in my unit tests (I cannot stress the importance of good unit testing!).</p>
<p>My backend calculation was basically this (simplified):</p>
<pre lang='ruby' class='prettyprint'>
# arbitrary amounts for these two variables
percentage = 12
total_in_cents = 400

discount = percentage.to_f / 100.0
total_in_float = total_in_cents.to_f * 100.0
new_price = (total_in_float * discount ).round / 100
</pre>
<p>Now, it&#8217;s pretty obvious that 12% of (400 cents) $4.00 should just be $0.48 (48 cents)<br />
However, my barrage of unit tests kept producing strange results where a simple calculation was returning incorrect results. Doing some research, I discovered a series of articles worth reading including:</p>
<ul>
<li> <a href="https://makandracards.com/makandra/1505-invoices-how-to-properly-round-and-calculate-totals">How to properly round and calculate totals</a> </li>
<li><a href="https://makandracards.com/makandra/1178-bigdecimal-arithmetic-in-ruby">BigDecimal arithmetic in Ruby</a></li>
</ul>
<p>Also, check out the <a href="https://github.com/collectiveidea/money">Money gem</a> &#8211; I&#8217;ve never used it personally, but <a href="http://stackoverflow.com/questions/1019939/ruby-on-rails-best-method-of-handling-currency-money">people have said good things about it</a>. </p>
<p>Heeding the advice I found online, I re-wrote all my money-related calculations using BigDecimals instead of Floats.</p>
<pre lang='ruby' class='prettyprint'>
percentage = 12
total_in_cents = 400
discount = BigDecimal(percentage.to_s) / 100
total_in_float = BigDecimal(total_in_cents.to_s) * 100
new_price = (total_in_float * discount ).to_i / 100
</pre>
<p>After switching over from Floats to BigDecimals, my unit tests all passed!<br />
Lesson learned and hope this heads-up helps you guys too. </p>
<p><strong>Summary:</strong><br />
<strong>Use BigDecimals for money calculations and remember to write good UNIT TESTS!!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/ruby-and-rails/ruby-floats-bigdecimals-and-money-currency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git &#8211; Push a branch to remote repository</title>
		<link>http://qugstart.com/blog/git-and-svn/git-push-a-branch-to-remote-repository/</link>
		<comments>http://qugstart.com/blog/git-and-svn/git-push-a-branch-to-remote-repository/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 20:23:00 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Git and SVN]]></category>
		<category><![CDATA[branch]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[remote]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=145</guid>
		<description><![CDATA[So you just created a new branch locally. You put in 200 hours into this branch, and then realize that the only copy of all these changes is on your MacBook! Don&#8217;t get scared, just push the branch to your remote server.
It&#8217;s easy:

$> git push -u &#60;remote-name&#62; &#60;branch-name&#62;

Or, usually:

$> git push -u origin branch-name

]]></description>
			<content:encoded><![CDATA[<p>So you just created a new branch locally. You put in 200 hours into this branch, and then realize that the only copy of all these changes is on your MacBook! Don&#8217;t get scared, just push the branch to your remote server.</p>
<p>It&#8217;s easy:</p>
<pre class='prettyprint' lang='bash'>
$> git push -u &lt;remote-name&gt; &lt;branch-name&gt;
</pre>
<p>Or, usually:</p>
<pre class='prettyprint' lang='bash'>
$> git push -u origin branch-name
</pre>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/git-and-svn/git-push-a-branch-to-remote-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 3 RSpec Request Spec &#8211; Testing Subdomains</title>
		<link>http://qugstart.com/blog/ruby-and-rails/rails-3-rspec-request-spec-testing-subdomains/</link>
		<comments>http://qugstart.com/blog/ruby-and-rails/rails-3-rspec-request-spec-testing-subdomains/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 22:43:56 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Rails Testing]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[Rails 3]]></category>
		<category><![CDATA[Subdomains]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=138</guid>
		<description><![CDATA[How do you test sub-domains in RSpec Request specs (integration tests) ???

# Pass it into the GET request!
get '/programs/100', nil, {'HTTP_HOST' => 'sub.domain.com'}

The 3rd parameter to the get method is a hash of HTTP headers.
See the Rails API documentation for details.
Note:
Depending on the type of test you are working with (support / controller / request [...]]]></description>
			<content:encoded><![CDATA[<p>How do you test sub-domains in RSpec Request specs (integration tests) ???</p>
<pre class='prettyprint' lang='ruby'>
# Pass it into the GET request!
get '/programs/100', nil, {'HTTP_HOST' => 'sub.domain.com'}
</pre>
<p>The 3rd parameter to the get method is a hash of HTTP headers.<br />
See the <a href="http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get">Rails API documentation</a> for details.</p>
<p>Note:<br />
Depending on the type of test you are working with (support / controller / request / integration etc.) you pass in the sub domain differently.</p>
<p>Here&#8217;s some good reference posts on Stack Overflow for setting subdomains in <strong>controller</strong> specs:<br />
1. <a href="http://stackoverflow.com/questions/2556627/rails-rspec-set-subdomain">Rails RSpec Set Subdomain</a></p>
<pre class='prettyprint' lang='ruby'>
# Set the @request.host in a before block
before(:each) do
  @request.host = "#{mock_subdomain}.example.com"
end
</pre>
<p>2. <a href="http://stackoverflow.com/questions/6033717/my-rails-3-applicaiton-has-to-have-a-subdomain-to-work-my-rspec-controller-test">Subdomains in RSpec Controller Tests</a></p>
<pre class='prettyprint' lang='ruby'>
 # I haven't tried this, and not sure you would need to mock out the current_subdomain method.
  @subdomain = 'sub.domain.com'
  controller.expects(:current_subdomain).returns(@subdomain)
  @request.host = "#{@subdomain}.test.host"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/ruby-and-rails/rails-3-rspec-request-spec-testing-subdomains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

