<?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>Chasing</title>
	<atom:link href="http://chase.ratchetsoftware.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chase.ratchetsoftware.com</link>
	<description>Chase Gray's blog with solutions to various problems by a curious american Ph.D. student.</description>
	<lastBuildDate>Thu, 04 Mar 2010 05:12:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reading Advantage Database Server Files (.ADT) in Ruby</title>
		<link>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/</link>
		<comments>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 01:45:50 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby-ADT]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=172</guid>
		<description><![CDATA[I recently received some data in the Advantage Database Server file format (.adt).  In the past I have worked with DBF, Access, and .xls.  I was able to find some way to retrieve data from these formats using open source software.  The open source solutions were much easier to work with, usually [...]]]></description>
			<content:encoded><![CDATA[<p>I recently received some data in the <a href="http://www.sybase.com/products/databasemanagement/advantagedatabaseserver">Advantage Database Server</a> file format (.adt).  In the past I have worked with DBF, Access, and .xls.  I was able to find some way to retrieve data from these formats using open source software.  The open source solutions were much easier to work with, usually not requiring any sort of driver installation, etc.</p>
<p>I searched thoroughly for an easy way to retrieve information from this set of .ADT files I was dealing with.  Sybase provides tools to work with the data, but all of them require using their software or drivers developed by them.  I would have settled for this if it wasn&#8217;t such a hassle to figure out how to use them.  I asked the question on <a href="http://stackoverflow.com/questions/2289519/automate-conversion-of-sybase-adt-files-to-sql">Stackoverflow.com</a> and got an answer from one of the employee&#8217;s of Sybase.  While helpful, the suggested Perl driver was one of the things I was avoiding.  I did attempt it and after a little hassle, moved on to creating a simpler solution.</p>
<p><span id="more-172"></span>One solution was to simply use Sybase&#8217;s provided &#8220;<a href="http://devzone.advantagedatabase.com/dz/content.aspx?key=20&amp;Release=13">Advantage Data Architect</a>&#8221; software to open the .ADT files and export them all to SQL.  This is possible with their software, but it would not work for my situation.  The developers of the program outputting these .adt files used a separate embedded database for each new report that was being worked on.  This meant there was hundreds of folders, each containing 30-40 .ADT files making up their own embedded database.  I needed something that could automate the extraction of this data.</p>
<h2>Data Format</h2>
<p>To cut this short, I decided to open up the .ADT files with a hex editor and see if I could get the information I needed out.  I only needed the column names, their types, and the actual rows of data.  To aid anybody else doing something similar to this, I&#8217;ll include some of my notes about the format here:</p>
<ul>
<li>The data is stored at the very end of the file. </li>
<li>The byte offset when the data starts is located at byte 32 and is 32-bits</li>
<li>Byte 24 contains a 32-bit integer specifying the number of rows</li>
<li>Byte 36 contains the a 32-bit number representing the number of bytes each record consumes</li>
<li>The header is 400 bytes</li>
<li>Column information is after the header, before the data.  Each column entry is 200 bytes.</li>
<li>The number of columns can be calculated (data_offset-400)/200</li>
<li>Column names consume the first 128 of each column entry.</li>
<li>Byte 130 contains a 16-bit integer representing the type of data.</li>
<li>Byte 136 contains a 16-bit integer, the length of the field in bytes</li>
<li>Character = 04, Double = 0A, Integer = 0B, Shortint = 0C, CICharacter = 14, Date = 03, Time = 0D, Timestamp = 0E, Autoinc = 0F</li>
</ul>
<p>This was enough information to get what I wanted.</p>
<h2>Ruby-ADT Gem</h2>
<p>I took the existing <a href="http://github.com/infused/dbf">DBF gem</a> by Keith Morrison.  Most of the credit for this gem goes to him as I borrowed the structure heavily from his gem.</p>
<p>I&#8217;ll provide an example of what is now possible with my new ADT gem.  No drivers are required and it&#8217;s as simple as installing the gem.  I&#8217;ll include a snippet of the readme from my <a href="http://github.com/chasemgray/Ruby-ADT">Ruby-ADT gem</a> on github, borrowed heavily again from the DBF gem.</p>
<h3>Installation</h3>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">gem install ruby<span style="color:#006600; font-weight:bold;">-</span>adt</pre></div></div>

<h3>Basic Usage</h3>
<p>Load an ADT file:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'adt'</span>
&nbsp;
table = <span style="color:#6666ff; font-weight:bold;">ADT::Table</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test.adt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Enumerate all records</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>          
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">name</span>          
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">email</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Load a single record using <tt>record</tt> or <tt>find</tt></p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Attributes can also be accessed through the attributes hash in  original or underscored form or as an accessor method using the underscored name.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;PhoneBook&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;phone_book&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">phone_book</span></pre></div></div>

<p>Search for records using a simple hash format.  Multiple search  criteria are ANDed. Use the block form of find if the resulting recordset could be  large otherwise all records will be loaded into memory.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># find all records with first_name equal to Keith</span>
&nbsp;
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'Keith'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>  
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">last_name</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find all records with first_name equal to Keith and last_name equal to Morrison</span>
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Keith'</span>, <span style="color:#ff3333; font-weight:bold;">:last_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'Morrison'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>   
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">last_name</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find the first record with first_name equal to Keith</span>
&nbsp;
table.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Keith'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find record number 10 </span>
&nbsp;
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Migrating to ActiveRecord</h3>
<p>An example of migrating a DBF book table to ActiveRecord using a  migration:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'adt'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CreateBooks <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>      
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>            
		table = <span style="color:#6666ff; font-weight:bold;">ADT::Table</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'db/adt/books.adt'</span><span style="color:#006600; font-weight:bold;">&#41;</span>            
		<span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>table.<span style="color:#9900CC;">schema</span><span style="color:#006600; font-weight:bold;">&#41;</span>        
		table.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>                  
			Book.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>record.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#41;</span>            
		<span style="color:#9966CC; font-weight:bold;">end</span>      
	<span style="color:#9966CC; font-weight:bold;">end</span>  
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>        
		drop_table <span style="color:#ff3333; font-weight:bold;">:books</span>    
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Limitations and known bugs</h3>
<ul>
<li>ADT is read-only</li>
<li>External index files are not used</li>
<li>Dates are not currently handled correctly.</li>
</ul>
<h2>Future Directions</h2>
<p>I am going to continue working on this library.  I have already heard from a few others that found this useful immediately after I posted it online.  I was surprised people could find it so quickly.  I&#8217;m sure there are bugs.  I would appreciate any problems to be reported to the <a href="http://github.com/chasemgray/Ruby-ADT">Ruby-ADT Github page</a>.  Emails are also welcome, but any issues should be directed to the github page and I&#8217;ll get to them when I can.</p>
</p>
<p>Hope this helps somebody out there dealing with this format.</p>
</p>
<p>- Chase Gray</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Converting Multiple Microsoft Access Databases into a Single MySQL Database &#8211; Rails, Ruby, MDBTools</title>
		<link>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/</link>
		<comments>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 21:04:25 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=168</guid>
		<description><![CDATA[Some of the data I&#8217;ve been dealing with lately is in Microsoft Access databases (.mdb files).  I&#8217;ve been surprised with the lack of support for manipulating these files outside of Access.  There are a couple solutions, but nothing satisfied me needs.  The worst data set came to me in a few hundred [...]]]></description>
			<content:encoded><![CDATA[<p>Some of the data I&#8217;ve been dealing with lately is in Microsoft Access databases (.mdb files).  I&#8217;ve been surprised with the lack of support for manipulating these files outside of Access.  There are a couple solutions, but nothing satisfied me needs.  The worst data set came to me in a few hundred different .mdb files.  It looked like the person that exported the database exported a copy of the database for each unique facility.  So each facility had a file, but the schema for each file was exactly the same.  It was apparent that in order to do any sort of complex queries on the data, the files would have to be combined.  I was recommended a tool called mdbtools by somebody at a local Ruby meetup.  I ended up using this along with Ruby inside of a Rake task to convert all my .mdb files into a single MySQL database.  <span id="more-168"></span></p>
<h3>Prerequisites</h3>
<p>The first thing I needed to do was to successfully configure and compile mdbtools. This probably took the longest as there are a few different version out there.  I had the most success with these instructions from the author&#8217;s git repository, <a href="http://www.automatthew.com/2008/02/how-to-compile-mdbtools-on-mac-os-x.html?showComment=1219246620000">http://www.automatthew.com/2008/02/how-to-compile-mdbtools-on-mac-os-x.html?showComment=1219246620000</a>. I was also able to compile this version on my ubuntu machine pretty easily.  The biggest hangup was with a couple of dependencies.  I also had a weird error where the autogen.sh was not detecting my libtools library, but I knew this was installed.  I commented out this check in the autogen.sh and things all worked, luckily.</p>
<p>Understanding mdb-tools was not very difficult.  It contains various libaries for interfacing with it through C, etc.  The only thing I cared about were the command line tools that it provided, listed <a href="http://mdbtools.sourceforge.net/install/x53.htm">here</a>. The commands I will use in this post are mdb-tables, mdb-schema, mdb-export.</p>
<p>The other requirement is to have a MySQL table created ready with permissions, etc. that will end up holding the data from the combined .mdb files.</p>
<h3>Step 1: Extracting the Schema</h3>
<p>One important thing to note about my situation is that all the .mdb files have the same schema.  Knowing this, I was able to simply find a random .mdb file and base my MySQL schema on this.</p>
<p>I have my .mdb files stored in a shared directory and so within my Rake task I used the following code to grab the first one (I&#8217;m sure there is a better way).</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">first_mdb_file = <span style="color:#996600;">''</span>
<span style="color:#008000; font-style:italic;">#get first mdb file</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">entries</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>mdb_file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> mdb_file =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">mdb</span><span style="color:#006600; font-weight:bold;">/</span>
    first_mdb_file = <span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/#{mdb_file}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">break</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Knowing the path to the first .mdb file the schema can be extracted by getting the result of the following shell command (the code surrounded in backticks is executed on the command line of the system).  The -S flag tells it to santize some of the table names by replacing spaces with underscore, etc.  I needed this for my tables because there were some problems without it.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">schema_sql = <span style="color:#996600;">`mdb-schema -S #{first_mdb_file} mysql`</span></pre></div></div>

<h3>Step 2: Building the Tables</h3>
<p>Since I&#8217;m doing this in a Rake task in Rails (you could do this in plain ruby), I am using an ActiveRecord connection to interface with my MySQL database.  I have a class called ExternalDatabase that allows me to connect to a different database without modifying the one that my main Rails program uses.  I will list it below, I don&#8217;t remember exactly where I got it from.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#Class used to connect to external databases without modifying the exiisting connection used by Rails</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">abstract_class</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">set_source</span><span style="color:#006600; font-weight:bold;">&#40;</span>configuration_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    source = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">configurations</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;sources&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>configuration_name.<span style="color:#5A0A0A; font-weight:bold;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    establish_connection source
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">set_configuration</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span>
    source = config
    establish_connection source
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see, I also have a section in my database.yml where i list connection details for various source databases.  This isn&#8217;t related to this post though.</p>
<p>So, assuming we have this class, we can use the following code to load our new schema into out MySQL database.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span>.<span style="color:#9900CC;">set_configuration</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:adapter</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;mysql&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:host</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;localhost&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:username</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_USERNAME,
<span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_PASSWORD,
<span style="color:#ff3333; font-weight:bold;">:database</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_NAME,
<span style="color:#006600; font-weight:bold;">&#41;</span>
db =  <span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span>.<span style="color:#9900CC;">connection</span> 
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;adding if exists clause to drop tables&quot;</span>
&nbsp;
schema_sql = schema_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>DROP TABLE <span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'DROP TABLE IF EXISTS <span style="color:#000099;">\1</span>;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
schema_sql = schema_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/--</span>.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;executing schema sql&quot;</span>
lines = schema_sql.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">';'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
lines.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sql_line<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> sql_line.<span style="color:#9900CC;">blank</span>?
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql_line<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The first part builds my database configuration hash that I pass in to ExternalDatabase.  I save the connection into a variable called &#8216;db&#8217;.  One of the important things to note about this and mdb-tools is that it does not check if the table currently exists before trying to drop the table.  So I added a snippet of code that removes the comments and adds &#8216;IF EXISTS&#8217; to the &#8216;DROP TABLE&#8217; clause in the schema sql.</p>
<p>For some reason, trying to execute this entire schema sql string with db.execute causes various errors that I couldn&#8217;t figure out.  By separating the schema sql into individual sql statements terminated with semicolons seemed to fix this problem.  The last bit of code does the separation into lines and executes the sql on the database. After all this, the database should have the correct schema.</p>
<h3>Step 3: Load Data</h3>
<p>One of the last things we want to do is actually load all the data from the different mdb files into the single MySQL database.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">tables = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">`mdb-tables #{first_mdb_file}`</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">entries</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>mdb_file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> mdb_file =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">mdb</span><span style="color:#006600; font-weight:bold;">/</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Current file: #{mdb_file}&quot;</span>
    tables.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>table<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Current table: #{table}&quot;</span>
      insert_sql = <span style="color:#996600;">`mdb-export -I -S #{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/#{mdb_file} #{table}`</span>
      insert_sql = insert_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>;<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      insert_sql = insert_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span>?\<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\s<span style="color:#006600; font-weight:bold;">+</span>INSERT<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'<span style="color:#000099;">\1</span>;<span style="color:#000099;">\2</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      lines = insert_sql.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">';'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      lines.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sql_line<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> sql_line.<span style="color:#9900CC;">blank</span>?
        db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql_line<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So the first line runs mdb-tools command line tool &#8216;mdb-tables&#8217; to get a list of the tables separated by spaces.  I could have done this through my previously created schema as well.</p>
<p>This code goes through every .mdb file in my directory and uses mdb-tools to extract the data as sql &#8216;INSERT&#8217; statements from the command-line.</p>
<p>In the inner-most loop, I clean up the returned value from mdb-export because it does not include semi-colons after each INSERT statement.  I also remove any semi-colons before adding any because some of my fields had semi-colons in them.  Again, I separate each INSERT statement into a single statement and execute these individually in the last section of the code.</p>
<p>After all this you should have all your data combined into a single MySQL database.  One problem might exists though.  Certain tables, such as tables that contain general purpose labels, etc. for other tables to refer to, might contain duplicates.  This wouldn&#8217;t have caused a big problem for me because I just needed to extract data, but if you needed to continue using this database you would need to remove the duplicates.</p>
<h3>Final Step: Removing Duplicate Rows</h3>
<p>There seem to be a lot of different ways to remove duplicate rows from a MySQL table.  I wish there was a simple solution, but I couldn&#8217;t find one.  The following code does the following:</p>
<ol>
<li>Create a temporary table that has the same schema as the table we are removing duplicates from.</li>
<li>Insert all distinct rows into this new temporary table.</li>
<li>remove all values from the original table.</li>
<li>Insert all of the rows from the temporary table back into the original table.</li>
<li>Finally, drop the temporary table.</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">tables.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>table<span style="color:#006600; font-weight:bold;">|</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;CREATE TABLE temp LIKE #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;INSERT INTO temp select distinct * FROM #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;TRUNCATE #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;INSERT INTO #{table} select * from temp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;DROP TABLE temp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Thanks for reading, I hope this was helpful to others that have some similar problems.</p>
<p>- Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Treat Google Referred Users Special</title>
		<link>http://chase.ratchetsoftware.com/2009/06/treat-google-referred-users-special/</link>
		<comments>http://chase.ratchetsoftware.com/2009/06/treat-google-referred-users-special/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 05:56:54 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=155</guid>
		<description><![CDATA[The content of my main web application, Myhealthcaresource, will contain more than 15000 detailed financial reports for nursing facilities at its future peak.  Each of these reports contains textual information as well as monetary values.  It might list administrator names, employee names/salaries, owners, products or services purchased.  I wanted to have all of this information [...]]]></description>
			<content:encoded><![CDATA[<p>The content of my main web application, <a title="Myhealthcaresource" href="http://www.myhealthcaresource.com">Myhealthcaresource</a>, will contain more than 15000 detailed financial reports for nursing facilities at its future peak.  Each of these reports contains textual information as well as monetary values.  It might list administrator names, employee names/salaries, owners, products or services purchased.  I wanted to have all of this information searchable on Google, without Google caching the page.  I also wanted to let users who came from Google as a result of searching for this information see it without having to log in, but only the page that they found through Google search.<span id="more-155"></span>I&#8217;m going to make this post much shorter and only list the steps I took to solve these problems.</p>
<h3>Problem 1: How to let Google index my data that requires a login</h3>
<p>A prerequisite for this step is to submit sitemaps for your site&#8217;s content to <a href="https://www.google.com/webmasters/tools">Google Webmaster Tools</a>.  Once you have done this, at some point in the future Googlebot will attempt to traverse your page and index its contents (hopefully).</p>
<p>The content on my site requires a user to pay a subscription fee in order to have free reign and browse through whatever they please.  I&#8217;m not very concerned about any single piece of information getting leaked, instead I&#8217;m protecting the resource the site provides as a whole.  By letting Google index your site, you&#8217;re obviously opening up your content to outside viewers that are not logged in.  The extent that you need to protect your content could vary from what my needs are.</p>
<p>What I want is for a user to be able to type their name into Google search engine and find out that they are referenced in the data provided by Myhealthcaresource.com.  So, I need to somehow let Googlebot traverse my site, even through it requires a login by a normal user.</p>
<p>I&#8217;m using Restful Authentication and a role requirement system, so this is how I include this exception.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FacilitiesController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; ApplicationController
 require_role <span style="color:#ff3333; font-weight:bold;">:basic</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:unless</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'HTTP_USER_AGENT'</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>Googlebot.<span style="color:#006600; font-weight:bold;">*/</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This could vary depending on your authentication system, but the part that will remain the same is the check if the user agent string contains &#8216;Googlebot&#8217;.</p>
<p>That&#8217;s all it takes in order to allow Googlebot to traverse my controlelr that previously required a login.  A potential problem that could come up is that users could &#8216;fake&#8217; their user agent to say &#8216;Googlebot&#8217; and gain access to the site.  This could be a problem for some sites, and we would keep an eye out for this in our logs or through analytics software.  We provide a free trial to all our users before they decide to pay, so uers get a free look at the data already and there is no reason to try to sneak around the site to see it.  We&#8217;re not concerned with this problem, but it could be an issue for others.</p>
<h3>Problem 2: How to prevent Google from caching private pages</h3>
<p>Once Google starts indexes pages of your site that require a login for a normal user, it will also start caching all the sites that it visits.  If you are worried about your internal content getting cached by Google you will want to prevent Google or any other robots from caching.  This is simple and is accomplished by putting the following in the heading of your main layout file.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;META name=&quot;robots&quot; content=&quot;index,follow,noarchive&quot; /&amp;gt;
&amp;lt;META NAME=&quot;googleBOT&quot; CONTENT=&quot;noarchive&quot; /&amp;gt;</pre></div></div>

<p>When Googlebot reaches your page and sees this tag, it will not cache it.  I have this on the Myhealthcaresource layout, and by searching <a href="http://www.google.com/search?q=myhealthcaresource&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a">myhealthcaresource in google</a> you can see there are no cache links.</p>
<h3>Problem 3: Allowing Users Referred From Google View Content Without Logging In</h3>
<p>When a user sees a link to myhealthcaresource on a set of Google search results, they are most likely to the detailed facility reports that I submitted in a sitemap.  They probably searched their company name or their own and found that they were referred to by my site&#8217;s information.  When someone clicks this link in Google, by default they would get redirected to a page asking them to log in.  This would make almost every user referred from Google resort to their back button immediately.  I noticed this behavior during the period when my site functioned this way.  These users from Google have no idea what kind of information our site offers and they might be a potential customer, so we don&#8217;t want them to leave because we present a log in form when they first arrive.</p>
<p>An alternative is to allow users referred to our site through a Google search to view the entire page.  We decided to do just this.  When they click the link in Google&#8217;s search results they are not asked to log in and they are shown the same page a logged in user would see.  What happens when they try to browse the rest of the private content?  They are asked to log in, but we already have their interest at this point and they are more likely to stay if they found something that was useful to them on the page they landed on.</p>
<p>So how do we accomplish this?  In a similar way that we allowed Googlebot to traverse our pages, we can allow users that were referred from Google to access our page:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FacilitiesController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; ApplicationController
  require_role <span style="color:#ff3333; font-weight:bold;">:basic</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:unless</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> request.<span style="color:#9900CC;">referer</span> =~ <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>google.<span style="color:#006600; font-weight:bold;">*/</span> <span style="color:#006600; font-weight:bold;">||</span> request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'HTTP_USER_AGENT'</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>Googlebot.<span style="color:#006600; font-weight:bold;">*/</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Of course this code would vary slightly if you are using a different authentication scheme.  The core idea would be the same though.  Now, the potential problem that could come up with this is that a malicious user uses Google to repeatedly access your site by creating clever search queries.  In our case, if a user is this intent on stealing the data, we weren&#8217;t likely to win them as a customer in the first place.  We can also monitor this through analytics tools and if it is abused we will look into an alternative solution.  It could be possible to restrict the number of Google referrals a certain IP can have in a time period or something.</p>
<p>I hope that some of this work can be useful to people who are looking for ways to leverage Google for their dynamically generated login-only content.  We see people come from Google searches every day, and now they stay much longer as a result of these changes.</p>
<p>In a future post, I will briefly talk about another method our site uses to send links to users that allow them to browse the site freely without logging in.  After a certain time period these links expire and the users must sign up to browse the site further.</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2009/06/treat-google-referred-users-special/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Uniq for Array or Hash with a Deeply Nested Structure</title>
		<link>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/</link>
		<comments>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 23:32:56 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=148</guid>
		<description><![CDATA[Most people have had some experience with ruby&#8217;s built in #uniq method for Arrays.  Internally, this method finds the unique items in the array by creating a hash internally, and this internal comparison is done with the #eql? method.  If an item in the array is a Hash, then #eql? simply uses the [...]]]></description>
			<content:encoded><![CDATA[<p>Most people have had some experience with ruby&#8217;s built in #<a title="Array Uniq Method" href="http://www.ruby-doc.org/core/classes/Array.html#M002237">uniq method for Arrays</a>.  Internally, this method finds the unique items in the array by creating a hash internally, and this internal comparison is done with the #eql? method.  If an item in the array is a Hash, then #eql? simply uses the object_id, generated by <code>the #hash method, </code>to determine whether it is equal to another object in the array.  There are many solutions online each with s light variations and goals.  I found myself in need of a uniq method for an array containing items in an arbitrarily deep nested structure (ie many sub-hashes and arrays). <span id="more-148"></span></p>
<p>I found myself working with some data in a hash generated from scraping a website.  The website had some duplicate content and I didn&#8217;t feel like it was necessary to clutter my scraping code with tests for duplicate content while it was scraping.  After scraping the data and turning it into a hash, I wished to remove the duplicates with some post-processing.  I felt like this should be pretty simple, except that the elements I wanted to test for uniqueness contained a deeply nested structure of hashes and arrays.  Here is an example an element in an array I wanted to uniqify:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:posts</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'First Post!'</span> <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:commenter <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Alice'</span>, <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Hi bob!'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The #uniq method does not work as expected when there is a Hash element in the array, it only uses the object_id to determine uniqueness. There are some solutions online for dealing with this.</p>
<p>If you know that each element inside the array is a hash with no sub-hashes you can use the solution presented in the answer of <a href="http://stackoverflow.com/questions/181091/how-do-i-get-the-unique-elements-from-an-array-of-hashes-in-ruby">this</a> stackoverflow.com question.  The first solution uses #inject to build a new array with only unique items.  Whether an item is included in the array already or not is determined with the Hash&#8217;s #include method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>result,h<span style="color:#006600; font-weight:bold;">|</span> result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> h <span style="color:#9966CC; font-weight:bold;">unless</span> result.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>; result <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The second solution presented in the same post:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">|</span> h.<span style="color:#9900CC;">to_a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">uniq</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span>k <span style="color:#006600; font-weight:bold;">=&gt;</span> v<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>This flattens all of your hashes into an array.  It then calls uniq on the resulting array after all the hashes are flattened.  This means that for an item to be unique it must have the same key AND value.  After finding the unique elements, it then maps the items back into a hash.</p>
<p>In <a href="http://mikeburnscoder.wordpress.com/2008/01/18/uniquify-an-array-of-hashes-in-ruby/">this</a> post, Mike Burns solve this problem by redefining #hash and #eql?.  If you are doing this kind of comparison often, this would probably be a better approach.  He does note though, that by doing this the #dup method no longer works as expected.</p>
<p>So it was obvious through searching that I was going to have to figure this out on my own.  I was a little worried that I would have to implement an ugly recursive solution that searches the entire tree to find all duplicates.  While looking into this solution, I felt that it would be very nice if #uniq would accept a block that would allow me to return true if the given parameter is a duplicate, thus removing it from the returned array.  This is not implemented in my current ruby version (1.8.6), but there is an example of how to do this on <a href="http://redhanded.hobix.com/bits/klemmeSSilentHash.html">this blog post</a>.  Hopefully, more (all) of these methods in the ruby core will accept blocks someday.</p>
<p>I began to write a method that would search my tree-like structure of hashes and arrays and I felt like it was a very ugly solution.  I wanted something simple that would just take a line or two and get the job done.  So I showed an example of the problem to my non-computer scientist wife and she said, &#8220;why don&#8217;t you just compare how the two lists look when printed out?&#8221;  I was a little shocked that I hadn&#8217;t thought of this.  It isn&#8217;t the most efficient solution and it relies on the #inspect method, which is probably pretty slow because it has to determine the structure of all the hashes in order to print them in a readable format.   The old solution I was cooking up would also have to traverse the tree, similar to what I expected #inspect was doing.  In addition to the #inspect call, there would be a string comparison for each element.  I was willing to accept both of these for a short elegant solution to my problem.</p>
<p>The issue that came to my mind first was whether hashes get printed out the same way every time.  In Ruby 1.9, it seems that Hashes are going to be stored in the same order that they were entered.  In previous versions, I&#8217;m assuming that they are stored in the order of their generated hash.   This could cause a problem if the data someone was entering could be in different orders.  For my purposes though, the data would be entered in the same order if it was a duplicate.</p>
<p>So, after all of that, to uniqify an array containing a deeply nested structure, I present the following solution:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> nested_uniq
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span>item<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>other_item<span style="color:#006600; font-weight:bold;">|</span> item.<span style="color:#9900CC;">inspect</span> == other_item.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> nested_uniq!
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">replace</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">nested_uniq</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I&#8217;ll walk through it because it may seem a bit hard to understand at first.  I go through every element in the array with delete_if to determine if the element should be deleted from the array.  Then, for all elements except the one in question, I check to see if #any? of them are equivalent to the element in question.  This equivalence is determined by doing a string comparison of what the #inspect method returns for that element.</p>
<p>Here is an IRB test</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span>  test = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span>, <span style="color:#ff3333; font-weight:bold;">:test</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'10'</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'2'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:to <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1'</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'8'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span>, <span style="color:#ff3333; font-weight:bold;">:test</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'10'</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'2'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:to<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;1&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;8&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> test.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>test <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span>item<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>other_item<span style="color:#006600; font-weight:bold;">|</span> item.<span style="color:#9900CC;">inspect</span> == other_item.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:to<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;1&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;8&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Hope this is helpful to someone!  I am very interested to hear suggestions and comments on alternative ways to uniqify an array with elements that have a nested structure.</p>
<p>-Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Database Connection with Block</title>
		<link>http://chase.ratchetsoftware.com/2008/12/rails-database-connection-with-block/</link>
		<comments>http://chase.ratchetsoftware.com/2008/12/rails-database-connection-with-block/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 04:40:01 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=141</guid>
		<description><![CDATA[I found myself needing to very quickly connect to an alternate database that was defined in my database.yml like so:

alternate_data:
    adapter: mysql
    database: alternate_database
    username: user
    password: pass
    host: localhost

I was accessing it with something similar to the following:

ActiveRecord::Base.establish_connection&#40;ActiveRecord::Base.configurations&#91;&#34;#{state}_data&#34;&#93;&#41;
results = [...]]]></description>
			<content:encoded><![CDATA[<p>I found myself needing to very quickly connect to an alternate database that was defined in my database.yml like so:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">alternate_data:
    adapter: mysql
    database: alternate_database
    username: user
    password: pass
    host: localhost</pre></div></div>

<p>I was accessing it with something similar to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">configurations</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;#{state}_data&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
results = connection.<span style="color:#9900CC;">execute</span> description_params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:extraction_sql</span><span style="color:#006600; font-weight:bold;">&#93;</span>;
<span style="color:#008000; font-style:italic;">#This wasn't actually hard coded, but this is just an example</span>
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">configurations</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;development&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The problems with this was that I had to repeat this ugly code everywhere, and if there was an exception thrown while accessing the alternate database it would not revert back to the original before exiting.  So the next request to the server thought the default database was the alternate database.  What I decided would be useful would be to have an &#8220;establish_connection&#8221; method that accepted a block and handle these issues.</p>
<p><span id="more-141"></span></p>
<p>I am going to write about this because I planned to before realizing that in Rails 2.2 the exact same issue is solved already with a much better implementation (of course).  You can find how to solve this problem very elegantly in Rails 2.2 at <a title="Scaling ActiveRecord With MySQLPlus" href="http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/">http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/</a>.</p>
<p>My implementation is much simpler and I hope that anybody stuck with Rails 2.1 (like me for now) or any previous version can use it temporarily until they can upgrade in the future.</p>
<p>I just wanted a simple block establish_connection so I could write code like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">   establish_temporal_database_connection<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{state}_data&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>connection<span style="color:#006600; font-weight:bold;">|</span>
      results = connection.<span style="color:#9900CC;">execute</span> description_params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:extraction_sql</span><span style="color:#006600; font-weight:bold;">&#93;</span>;
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It would reset the old database connection if something went wrong or when the block was over.</p>
<p>I&#8217;m just going to list a simple module that does this, if anybody wants this in plugin form that actually overrides establish_connection then it isn&#8217;t that difficult to turn this into that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ConnectionWithBlock  
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extended</span><span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> object
        extend ClassMethods
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> establish_temporal_database_connection<span style="color:#006600; font-weight:bold;">&#40;</span>configuration<span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#008000; font-style:italic;">#save the old connection</span>
       previous_connection = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>
       <span style="color:#008000; font-style:italic;">#establish connection handling a possible exception and going back to original connection</span>
       <span style="color:#9966CC; font-weight:bold;">begin</span>
              <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">configurations</span><span style="color:#006600; font-weight:bold;">&#91;</span>configuration.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
              <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span><span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
              <span style="color:#CC0066; font-weight:bold;">raise</span> e
       <span style="color:#9966CC; font-weight:bold;">ensure</span>
              <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span><span style="color:#006600; font-weight:bold;">&#40;</span>ConnectionSpecification.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>previous_connection.<span style="color:#9900CC;">config</span>, previous_connection.<span style="color:#9900CC;">adapter_method</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#9966CC; font-weight:bold;">end</span>   
     <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/12/rails-database-connection-with-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Caching: Dynamic Fragments</title>
		<link>http://chase.ratchetsoftware.com/2008/12/rails-caching-dynamic-fragments/</link>
		<comments>http://chase.ratchetsoftware.com/2008/12/rails-caching-dynamic-fragments/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 03:03:51 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=134</guid>
		<description><![CDATA[I am finally getting around to implementing caching on my Rails application that I&#8217;ve been working on adamantly for the past couple of months.  For the time being, I am using a memcached server with simple action and page caching.  I ran into a few problems with my first few attempts at fragment caching and [...]]]></description>
			<content:encoded><![CDATA[<p>I am finally getting around to implementing caching on my Rails application that I&#8217;ve been working on adamantly for the past couple of months.  For the time being, I am using a memcached server with simple action and page caching.  I ran into a few problems with my first few attempts at fragment caching and decided it wasn&#8217;t really necessary for the time being.  I needed a simple method of including small sections of dynamic code in an otherwise static page.  I then wanted to use action caching on the resulting view.  I&#8217;ll outline a simple method I used to achieve this.</p>
<p><span id="more-134"></span></p>
<p>I&#8217;ve been very happy with the results.  Very server intense requests are responding instantly.  This is all great, but caching is usually only appropriate for pages with content that is static for a period of time.  The biggest problem I was having is that there are very small pieces of the pages I wished to cache that are dynamic.  For instance, I want to have a bar at the top that tells the user who is logged in and provides a set of buttons to do things to their profile, etc.  An example of this is shown below:</p>
<div id="attachment_136" class="wp-caption aligncenter" style="width: 478px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/12/picture-2.png" rel="lightbox[134]"><img class="size-full wp-image-136" title="Profile Summary Example" src="http://chase.ratchetsoftware.com/wp-content/2008/12/picture-2.png" alt="Small Dynamic Block Showing User Profile Links" width="468" height="111" /></a><p class="wp-caption-text">Small Dynamic Block Showing User Profile Links</p></div>
<p>So what I needed was an easy way to specify that this small block would be dynamic while still allowing the rest of the view to take advantage of action caching.  I&#8217;m sure there is some plugin that does this and more.  If there is a very good plugin that does just this, I would love to know about it in the comments.  For now, though I am going to provide my simple solution to the problem.</p>
<p>My solution was to create a partial for the profile summary.  I already had the block as a partial to make my view code more DRY.  Instead of this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>=render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'profile_summary'</span><span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>in my view, I replaced it with an AJAX call using Prototype (updated to use Richard Poirier&#8217;s suggestion in the comments):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    &lt;div id=&quot;info_panel&quot;&gt;
        <span style="color:#006600; font-weight:bold;">&lt;%</span>= javascript_tag remote_function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'profile'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'summary'</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;/div&gt;</pre></div></div>

<p>I then have a controller that can repond to this AJAX request for the profile.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> summary
  render <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
    page.<span style="color:#9900CC;">replace_html</span> <span style="color:#996600;">'info_panel'</span>, <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/profile/summary'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, every time the page loads, it will load the entire page using the cached version.  It will then make an additional POST request to get the dymanic content.  This would become more complicated if there were many dynamic sections that I needed to load.  I feel like if it was wrapped into a plugin that could automatically handle special dynamic fragments inside a block like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>=dynamic_fragment do<span style="color:#006600; font-weight:bold;">%&gt;</span>
   <span style="color:#006600; font-weight:bold;">&lt;%</span>=render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'profile_summary'</span><span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span>end<span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>it would make it very easy and intuitive to insert dynamic fragments into an otherwise static page.  I do not have the need to implement something like this yet, I am satisfied with my simple solution.  I would be very happy to find a solution where you can specify the dynamic sections of the view instead of only specifying the static portions.  Then the system would magically build the server-side AJAX responses without the user having to do anything.  This allows the use of action_caches which are supposed to be much faster than fragment caching.  While I&#8217;m super busy with my project, stopping to create plugins is not on the priority list, but by writing it all down here I might come back to the best ideas.</p>
<p>I hope anybody looking to insert a small section of dynamic code in an otherwise action cacheable page finds this information useful.</p>
<p>Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/12/rails-caching-dynamic-fragments/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>URL Archive System or: URL Hacking Made Easy</title>
		<link>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/</link>
		<comments>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 02:53:32 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=125</guid>
		<description><![CDATA[Have you ever found yourself slightly modifying a URL to try to find something you know used to exist or should exist but you keep getting that dreaded 404 page?  Perhaps you were trying to find something that shouldn&#8217;t be online anymore but it was simply unlinked to, benevolent purposes or otherwise? Another example might [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever found yourself slightly modifying a URL to try to find something you know used to exist or should exist but you keep getting that dreaded 404 page?  Perhaps you were trying to find something that shouldn&#8217;t be online anymore but it was simply unlinked to, benevolent purposes or otherwise? Another example might be that a site&#8217;s main homepage is down and so many users are unable to access the site.  If they had a list of URLs they could see which are still valid very quickly with the URL Archive and go to the site through those URLs.  <a href="http://www.googleguide.com/cached_pages.html">Google cache </a>and <a href="http://www.archive.org/web/web.php">Wayback Machine</a> get you pretty close to what you&#8217;re after but sometimes they just fall a little too short.  Some things are beyond even<em> </em>Google&#8217;s giant umbrella of web applications (at least for now).</p>
<p>I&#8217;m sure an application focusing more on URLs is somebody&#8217;s 10% project somewhere sitting on the backburner until they have time to finish it.  It seems like almost anything you&#8217;d want to do has a corresponding web application.  When I come across a need that doesn&#8217;t I just feel like that void should soon be filled.  I&#8217;m going to outline the application that I believe would fill this void.  Maybe in future posts I&#8217;ll also walkthrough my attempts to do it as my personal .5% project.<span id="more-125"></span></p>
<h3>Doesn&#8217;t this already exist?</h3>
<p>When giving the quick summary of what I envisioned for the project the first question I get is, &#8220;Doesn&#8217;t Google Cache already do that?&#8221;  My initial reaction is that if it did, I would already be using it for that purpose.  After thinking about it, it doesn&#8217;t seem like that far of a stretch for Wayback Machine or Google Cache to archive URLs as well.  I feel like the real kicker is that in order to make it useful there would have to be extra processing and utilities for the URL archive.  The information is already there, so what needs to be done is create an interface for analyzing the URL a site has.  Before talking about the ideal solution, first let&#8217;s look at what is offered by Google Cache and Wayback Machine already to compare.</p>
<p>To get this sort of functionality out of Google Cache as far as I know is to use a &#8220;site:&#8221; search.  So, typing in the search, &#8220;site:arena.cse.sc.edu&#8221;, I get the following:</p>
<div id="attachment_127" class="wp-caption alignnone" style="width: 510px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/11/picture-1.png" rel="lightbox[125]"><img class="size-full wp-image-127" title="ARENA Google Site: Result" src="http://chase.ratchetsoftware.com/wp-content/2008/11/picture-1.png" alt="Result for site:arena.cse.sc.edu" width="500" height="271" /></a><p class="wp-caption-text">Result for site:arena.cse.sc.edu</p></div>
<p>I know that our research group&#8217;s website has lists of most of the pdfs that we have published as well a number of static pages that total more than the 17 that are listed in these results.  Of course, this site isn&#8217;t really optimized for search engines, so that might also not be helping.  Searching for just pdfs returns a measly 7 results.  Some of our papers are listed as links to other sites such as IEEE, which causes them to not get listed in these results.  Alternatively, if I could somehow easily browse all the URLs that exists for this domain, I might be able to see more information about what the website actually contains internally (more on this later).  Another experiment was to search for, &#8220;site:chase.ratchetsoftware.com&#8221;, and because this site is optimized for search engines, most of the URLs that exist were found pretty easily.  There is still no way to really tell what the site contains as an overview without going to the site or reading the provided description.  Put another way, it would be interesting to be able to see a something like a content tree that lets a viewer easily see what is contained in the site with a quick glance.  These are all things I believe can be solved and would be useful, but Google Cache and Wayback Machine are not the tools to do it.</p>
<h3>What would an ideal solution look like?</h3>
<p>I&#8217;ve given a few examples where Google Cache and Wayback Machine don&#8217;t offer what I am looking for.  I think to improve my argument it is best to talk about what the URL archive system would be in a perfect world.  It&#8217;s hard to see the difference in something that doesn&#8217;t yet exist.</p>
<p>I am going to list the features I have in mind and then explain any that aren&#8217;t obvious:</p>
<ol>
<li>Provide a list of all URLs ever found for a given domain.</li>
<li>When a user views a list, it should check and display whether that URL is still valid.</li>
<li>Organize the list of URLs so that it is tractable by a normal user.  It should have collapsible SubURLs so that only top level URLs are in the master list.</li>
<li>Provide a filter so that only URLs matching the given filter are shown.</li>
<li>Provide the ability to click on a URL to make that the new root URL for our list.</li>
<li>A utility that can display a graphical tree view of the page and its links.  If the page links to an outside page, then show the link with the icon to that page if available.  This view would ideally provide enough information and &#8220;graphics&#8221; to give the user a good idea of what is available on the site.</li>
<li>If the site uses dynamic URLs, perhaps for a site written in rails or php, there should be some sort of analysis of the GET arguments from what the system can figure out.  This might be an area where users can submit additional information to improve the analysis of a web applications generated URL addresses.</li>
<li>One of the coolest ones.  A firefox plugin that automatically helps you when trying to figure out URLs based on what is stored in the URL Archive.  So if you&#8217;re trying to modify a URL slightly, you can get real-time help and see which URLs are still valid, etc.  Firefox is good right now with it&#8217;s new location bar that automatically searches your history and all that stuff.  It&#8217;d be even better if it could help you with long URLs that you haven&#8217;t typed in before!</li>
<li>Another plugin idea would be to provide screen readers easier access to a site&#8217;s content.  If all we had are the URLs and perhaps keywords for those URLs, a blind person might be able to more easily find content quicker.</li>
<li>One more plugin would be to have a sidebar while browsing sites, and as you browse you get an updated list of URLs that have existed at your current location on the site.  This would put a sidebar on your left with all the links you might see on that site and all the URLs that you might not see but existed in the past.  This would be useful for quickly navigating huge sites, especially ones that are moving their links around a lot.</li>
<li>The last idea I can think of right now is that the system could analyze how the URLs are changing and suggest to users URLs that may be used in the future or are currently used but not linked to based on the patterns in  the current URLs and their change history.  This might be useful if someone is trying to URL hack but doesn&#8217;t have any idea where to start.</li>
</ol>
<p>I feel like number six is the one that needs further explanation in order to seem as cool as it does to me.  I believe there are already existing tools that will show you a tree view of a website.  There are definitely tools to show you this information for your own site, cause I&#8217;ve used them before.  So how does number six differ from these tools?  The answer is that it doesn&#8217;t differ dramatically, but hopefully it would offer more information because the purpose is slightly different.  Let&#8217;s give an example of a user that is looking for a site that offers ebooks in pdf form online somewhere.  The user might go to various sites by searching through Google and waste time at many sites that seem to offer ebooks but don&#8217;t have that large of a selection.  If the user could easily view a graph of each site before visiting, and turned on a filter for &#8220;.pdf&#8221;, he could hopefully see a tree that branches out into many different URLs leading to ebooks he&#8217;s interested in.  He should be able to quickly get an idea of the quantity, quality, and link validity of the site&#8217;s offerings.  Each node could be clicked on to make that node the new root node in the tree to perhaps get more specific information about a certain path.   There are many possibilities for graphically browsing a site&#8217;s URLs and URL relationships.  To me, this seems like a separate application entirely.  It might be that the site should be developed with a SOAP or REST interface and another application could be developed that provides this tree view of websites.</p>
<h3>Potential problems and Questions</h3>
<p>Not all good things come from having a URL Archive.  All the <a href="http://searchsecurity.techtarget.com/news/article/0,,sid14_gci1315588,00.html">security concerns</a> that come with Google Cache and Wayback Machine will also affect the URL Archive.  Some information that people wished to remove from the internet by unlinking it might be affected in a negative way by a system that indexes URLs forever.  Analyzng the GET parameters of a web application might be good for some users, but a malicious user could probably use this information to make the website work as it wasn&#8217;t intended.  Most of these problems come with developing this type of application.  There are tradeoffs and we just need to try to do our best to mitigate them while still providing a good service.</p>
<p>Another issue is the fact that I have no idea how to store such a large amount of information as this system would require.  I don&#8217;t even know where to begin with the indexing of URLs on the Internet.  It seems like it would take way too long with this little virtual machine on a server somewhere that I have.  I&#8217;d love to hear some comments about this.  How would one create an application like this that has to store such large amounts of information and process so much new data all the time?  Some resources relating to this would probably make for some great bathroom pulp <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Call for help</h3>
<p>In my current situation I have way too much on my plate to try to develop an application for which there would be no immediate gain.  I need to work on my own business to try to get that going before I have to get a real job <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .  I also have an seemingly endless amount of Ph.D. work.  Despite this, I would more than willing to contribute to a project that was working on this solution.  I would even be happy to set up a Rails application on this server and with a Git repository if there were a couple developers interested in working on it.  All I&#8217;d need to distract me from my other work is a little motivation from other people&#8217;s interest, so please let me know in the comments if this is something you&#8217;d like to use or have needed in the past.</p>
<p>Thanks for reading,</p>
<p>- Chase Gray</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Google Interview or: How I Learned to Stop Worrying and Drink the Kool-Aid</title>
		<link>http://chase.ratchetsoftware.com/2008/09/drinking-the-kool-aid/</link>
		<comments>http://chase.ratchetsoftware.com/2008/09/drinking-the-kool-aid/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 23:42:14 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Jobs]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=67</guid>
		<description><![CDATA[Around November 2007 I managed to get into Google&#8217;s Boston office for an interview.  I had always wanted to interview at Google and this was the opportunity I had been waiting for.  I had applied many times in the past, but never got anything except a generated rejection letter.  Before I got my interview and [...]]]></description>
			<content:encoded><![CDATA[<p>Around November 2007 I managed to get into Google&#8217;s Boston office for an interview.  I had always wanted to interview at Google and this was the opportunity I had been waiting for.  I had applied many times in the past, but never got anything except a generated rejection letter.  Before I got my interview and while I was studying I searched endlessly online for any tips that might help me succeed and to know what to expect.  There are plenty of resources, and I&#8217;ll highlight a couple of them.  I&#8217;m not going to reproduce what&#8217;s already been said.  I&#8217;m just going to tell the story of my visit to the Google Boston office starting with my early attempts to get an interview and ending with the actual interview and my reactions to it.</p>
<p><span id="more-67"></span></p>
<h3><span>Failed Attempts</span></h3>
<p>I probably began hoping that I could one day work at Google at the very end of High School.  Back then I was focusing on Microsoft, but stories of the Google workplace soon overshadowed my dreams of working at Microsoft.  Looking back, I think each place has its pros and cons.  Without going to Microsoft for an interview, I don&#8217;t know if I can fairly compare the two.  I will say that I would enjoy making Google&#8217;s products more than Microsoft&#8217;s.  I feel like Google is making products that are pushing things forward, while Microsoft is stuck maintaining it&#8217;s current product line. Over time there began to be various videos and articles focusing on what it was like working at Google. I&#8217;ll include a couple here instead of wasting time talking about them.</p>
<h4>CNN Covers Working at Google</h4>
<p><!-- Smart Youtube --><span class="youtube"><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/4YA9QWceEfE&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" /><param name="allowFullScreen" value="true" /><embed wmode="transparent" src="http://www.youtube.com/v/4YA9QWceEfE&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="355" ></embed><param name="wmode" value="transparent" /></object></span></p>
<h4>Oprah Talks about Working at Google</h4>
<p><!-- Smart Youtube --><span class="youtube"><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/vd6BPhJjYL4&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" /><param name="allowFullScreen" value="true" /><embed wmode="transparent" src="http://www.youtube.com/v/vd6BPhJjYL4&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="355" ></embed><param name="wmode" value="transparent" /></object></span></p>
<p>It seems pretty hard to resist the ideal working environment for a young computer scientist.  I believe I applied to only Microsoft while I was still in high school because at the time it had the most popular(infamous) <a href="http://www.microsoft.com/college/ip_overview.mspx">internship program</a>.  Although it wouldn&#8217;t be long before Google interview stories began to become synonymous with Microsoft&#8217;s.  Of course looking back there was absolutely no reason for Microsoft to decide to interview me besides a very excited sounding cover letter.  I didn&#8217;t have stellar grades, I didn&#8217;t have any completed projects to show off, and I didn&#8217;t have any recommendations from anybody that had worked at Microsoft.  So of course I got a generic rejection letter from Microsoft a few months later.</p>
<p>Throughout college I could tell pretty much the exact same story except that my resume was growing every so slightly and I believe I quit applying to Microsoft after my sophomore year of college or around that time.  I focused on the <a href="http://www.google.com/support/jobs/bin/static.py?page=students.html&amp;sid=intern">Google internship</a> program because I felt that was where the exciting things were happening.  At first, if I remember correctly, the internship at Google was available to all college students.  At one points though, probably around my sophomore year again, I noticed that they started requiring you to be a Master&#8217;s or Ph.D. candidate.   Regardless, I still applied each Summer just in case I could get lucky.</p>
<h3>Landing the Interview</h3>
<p>As you can see in my introduction post, I started doing research with my academic adviser, <a href="http://www.cse.sc.edu/~srihari/">Srihari Nelakuditi</a>, during the Summer of my sophomore year as well.  This was when things seemed to be coming together and I was getting a little recognition for all the reading and programming I had been doing up until this point.  I was a step or two above the average peer in my classes, and I&#8217;m glad that Srihari could see some sort of potential.  I continued to apply every Summer for an internship at Google and also at <a href="http://www.fogcreek.com/Jobs/SummerIntern.html">Fog Creek Software</a>.  I had hopes that with my growing resume and research experience I might slip into an interview at one of these internships.  If I did not get the internships I was funded by my adviser to continue doing research in the <a href="http://arena.cse.sc.edu/">ARENA</a> lab over the Summer.  I continued at the University of South Carolina into my Graduate studies and got to the point where I was about to complete my Master&#8217;s thesis.  During my second to last semester as a Master&#8217;s student I got a lucky break.</p>
<p><a href="http://chase.ratchetsoftware.com/wp-content/2008/09/thumbs-up.jpg" rel="lightbox[67]"><img style="float: right;" title="Yay for references!" src="http://chase.ratchetsoftware.com/wp-content/2008/09/thumbs-up.jpg" alt="(Thumbs up) Yay for references!" width="100" height="100" /></a>Apparently my adviser had a few hookups at some of the companies I had been applying to over the years and was waiting until I was ready to refer me to them.  So, he told me one day that he gave me a reference to Google and I would be hearing from them soon.  It seemed kind of surprising that it should be so easy, but I&#8217;ve always read that these good jobs were about connections.  Sure enough&#8230;a few weeks later I got an email from a woman at Google recruiting.  This was the same location that had sent me all those generated rejection letters in the past.  I had a pretty similar resume this time, my experience only consisted of six extra months since the previous rejection, only this time I had someone else say, &#8220;Hey, hire this guy&#8221;.  It makes me wonder if I could have had only the reference and still be contacted by the recruiting department.</p>
<p>So, the first interesting thing someone might wonder who&#8217;s been attempting to get a job at a place like this is what do they say in an non-rejection email.  Well, here is the first email I got from the recruitment woman.</p>
<p><a href="http://chase.ratchetsoftware.com/wp-content/2008/09/google_recruitment_letter.jpg" rel="lightbox[67]"><img class="alignnone size-full wp-image-94" title="google_recruitment_letter" src="http://chase.ratchetsoftware.com/wp-content/2008/09/google_recruitment_letter.jpg" alt="" width="597" height="433" /></a></p>
<p><img src="file:///Users/chasegray/Desktop/thumbs-up.jpg" alt="" /></p>
<p>We continued talking about what kind of position I was looking for through a few email exchanges.  They were very nice and she even asked me about things in my extra-curriculum activities that were not related to Google, it seemed we had some similar interests.  For various reasons, I decided I didn&#8217;t want to go to California and instead let her know that I was interested in a Software Engineer full-time position at the Boston office.   I sent her my two page curriculum vitae and my current unofficial transcript and I was moved on to the next step.</p>
<p>So what came next was an email from a recruiter from the Google -- Boston office.  He immediately asked for a good time that he could call me and talk about various opportunities at his office.   Somehow I missed his first call because I believe he called my old phone for some reason.  The only explanation I had was that they had me in their database from previous resumes, and these old resumes had my old phone number on them.  So, all those old resumes had been getting into their system after all  <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_confused.gif' alt=':-?' class='wp-smiley' />  . This happened on more than one occasion and still confuses me why they kept using the old number.  He was a cheerful sounding guy who seemed to make every effort to put me at ease during the whole process.   I was worried at first this might involve some technical questions but I found out that it was just to talk to me and make some plans for the interview process.</p>
<p>The first step of most Google interviews is the infamous phone interview.  In this process you are supposedly asked questions by one or more engineers over the phone and you are supposed to talk your way through the problem.  There are a lot of examples of these online, so that&#8217;s where I&#8217;d recommend looking for them.  They are usually questions that aren&#8217;t exactly computer science, or slightly related, but lend themselves to solutions that could be spoken out over the phone.  The reason I don&#8217;t have any personal examples is because I got pretty lucky while talking to this guy on the phone.  It just so happened that my girlfriend and her parents were going to be going to Boston about a month later.  He mentioned something about a phone interview sometime later and I said that I would probably already be visiting Boston around that time.  I waited and hoped he would make the same conclusion that I had and he did.  He asked me if I would prefer to just come on in to the office when I was visiting then.  I was ecstatic , both because I didn&#8217;t have to take the phone interview and because I was going to Google&#8217;s office in Boston in less than a month!  I don&#8217;t know if I could recommend the same process to anyone else, but it worked for me.  I was going to have to pay for my hotel because I was arriving one day earlier than my girlfriend but it was OK with me.  They said I should come in at about 10:00AM and my interviews would last until about 2:15PM including lunch.  I asked about what to wear and I was told it&#8217;s pretty casual in the office so I should probably wear something semi-formal like a collared shirt and slacks.  Now all I had to do until the big day was to try and prepare for whatever questions might be asked of me during my technical interview.</p>
<h3>Preparation</h3>
<p>Like most people preparing for a technical interview, I didn&#8217;t have any idea where to begin.  It seemed like they could ask me anything.  Looking up examples of past questions from Microsoft and Google only made me more worried.  The questions I found were mostly reasonable, but some were just so weird and I had no idea how to solve them.  Regardless, one of the best ways to prepare was to study these example questions.  Not so much to memorize the answer, but to understand the thought process behind the answer.  Most of the questions people seem to face have similar fundamental problems.  The biggest challenge is to take a problem you are presented and trying to put it into terms similar to a problem you already know how to solve.  Many of the traditional problems could be found in most algorithms books.  It seems like algorithms pretty much dominate the questions, but all other areas of computer science are fair game.</p>
<p>I&#8217;ll try to list some of the resources I found.  I didn&#8217;t exaustively try to understand every problem, but I spent a while going over things that I didn&#8217;t understand and refreshing mysefl with fuzzy areas.  Again, the idea was to try and be capable of coming up with my own solution based on the knowledge I had, not to memorize answers.  Aside from studying algorithms and previous questions, my adviser also gave me a book with quick hacking tricks used in programming.  These ranged from how count the number of ones in a binary number quickly to various memory hacks.  I&#8217;ve seen that people were asked questions relating to these dirty hacks but they are mostly there to increase your bag of tricks.</p>
<ul>
<li><a href="http://www.acetheinterview.com/questions/cats/index.php/microsoft_google">Ace The Interview</a></li>
<li><a href="http://ofb.net/~niniane/interview_howto.html">Some good general tips for a technical interview from a Microsoft interviewer</a></li>
<li><a href="http://answers.google.com/answers/threadview/id/751886.html">Huge list of compiled questions from interviews.</a></li>
<li><a href="http://www.allinterview.com/">http://www.allinterview.com/</a></li>
</ul>
<h4>How to Hack the Technical Interview</h4>
<p><!-- Smart Youtube --><span class="youtube"><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/4KyCPiJPx48&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" /><param name="allowFullScreen" value="true" /><embed wmode="transparent" src="http://www.youtube.com/v/4KyCPiJPx48&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="355" ></embed><param name="wmode" value="transparent" /></object></span></p>
<p>After reading algorithms and technical interview questions for a few weeks during my free time.  I was finally getting prepared to hop on the plane to Boston for my interview.  I was very excited to get to see Boston for the first time.  It was also going to be when we told my girlfriend&#8217;s parents about our engagement, so it was going to go well even if my interview went bad.</p>
<h3>The Boston Office</h3>
<p>Arriving in Boston I got into my hotel near the airport and planned to stay in the entire night reviewing some basic algorithms.  I started to make sure I had everything to wear for the next day.  A nice collared shirt and black pants.  I realized at this point that I had forgotten to bring a belt, and the pants didn&#8217;t fit perfectly around my waist so it was a required piece.  I started to get pretty worried and was looking for a quick solution.  I decided to look online for the closest store.  I was near the airport so there was nothing at all.  I just got out of the hotel and walked on foot through East Boston for about 30-45 minutes until I came across an area that had some sort of stores in it.  I had very little hope though that I would find a belt for my outfit in what looked to be a mexican area of East Boston.   To my surprise I found a small mexican shop that sold clothes.  When looking through their limited supply of belts, I found that they all looked like this:</p>
<div id="attachment_106" class="wp-caption alignnone" style="width: 210px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/09/b150-bc-atn.jpg" rel="lightbox[67]"><img class="size-medium wp-image-106" title="b150-bc-atn" src="http://chase.ratchetsoftware.com/wp-content/2008/09/b150-bc-atn.jpg" alt="Tacky Belt" width="200" height="200" /></a><p class="wp-caption-text">Tacky Belt</p></div>
<p>This would obviously not do for my interview!  They did have one generic brown belt. I asked for it without trying it on at all and left.  I went to some pasta place for dinner and brought it back to the hotel.  When trying the belt on later that night I realized it was GIGANTIC!  I had to figure out a way to cut a hole in it and figure out a way to make it stay on and hold my pants up throughout the all day interview.  This was the biggest issue I faced during my entire time at Boston, so I think that makes my trip a relative success.</p>
<p>I woke up the next morning pretty early so I had time to figure out the train system and where I had to go.  I needed to get from East Boston over to Cambridge.  Obviously this wasn&#8217;t too much of an issue and pretty soon I was right next to the MIT campus looking for which building Google was located in.  The lady had given me the address, &#8220;One Broadway, Cambridge, MA&#8221;.  Apparently it was this big building here:</p>
<div id="attachment_99" class="wp-caption alignnone" style="width: 510px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/09/google_boston_office_far.jpg" rel="lightbox[67]"><img class="size-full wp-image-99" title="google_boston_office_far" src="http://chase.ratchetsoftware.com/wp-content/2008/09/google_boston_office_far.jpg" alt="Google Boston office from across the street" width="500" height="371" /></a><p class="wp-caption-text">Google Boston office from across the street</p></div>
<p>Walking up closer you can see that it is just a typical office building.  There was nothing flashy or fancy about the outside, but Google was located on the 7th floor (and I would learn also another floor as well).  I expected at least the 7th floor to have the &#8220;Google look&#8221;.</p>
<div id="attachment_100" class="wp-caption alignnone" style="width: 510px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/09/google_boston_office_close.jpg" rel="lightbox[67]"><img class="size-full wp-image-100" title="google_boston_office_close" src="http://chase.ratchetsoftware.com/wp-content/2008/09/google_boston_office_close.jpg" alt="Google Boston office front doors.  Then 7th floor." width="500" height="370" /></a><p class="wp-caption-text">Google Boston office front doors.  Then 7th floor.</p></div>
<p>For those of you who are truly interested, here is an embedded map of the location:</p>
<p><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=one+broadway+cambridge+ma&amp;sll=37.0625,-95.677068&amp;sspn=47.569986,110.742188&amp;ie=UTF8&amp;om=1&amp;t=h&amp;s=AARTsJpceGoIulIA0O5DwI-fholfrgYv0Q&amp;ll=42.368342,-71.086049&amp;spn=0.011097,0.018239&amp;z=15&amp;iwloc=addr&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=one+broadway+cambridge+ma&amp;sll=37.0625,-95.677068&amp;sspn=47.569986,110.742188&amp;ie=UTF8&amp;om=1&amp;t=h&amp;ll=42.368342,-71.086049&amp;spn=0.011097,0.018239&amp;z=15&amp;iwloc=addr&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small></p>
<p>I then used my extra time to sit at a nearby Cosi restaurant and get some breakfast and relax.  I was going to be in interviews for the next 4 or 5 hours.  So I finally went inside the building and up to the 7th floor to see what awaited me.</p>
<h3>The Interview (and Lunch)</h3>
<p>I walked off of the elevator to a big happy Google logo in the main hallway.  Then there were two glass doors that needed a card to unlock them.  Luckily someone was walking out and I walked in.  There was a lady at a desk to greet visitors and she had me sign in on a computer with a little application they had.  I had read somewhere that the Mountain View office has a big screen with real-time searches being displayed.  I had also read someone&#8217;s blog that the Boston office did not have one of these screens.  I was pleasantly surprised to find that there it was in all its glory to entertain me while I sat on one of the fancy Google colored chairs amidst the various toys and Google exercise balls on the floor.  As I looked around there were a couple offices (one with guitar hero) and a surprising amount of women.  As I would later learn, this was primarily the sales floor, which explains the large number of women.  Finally, my recruiter came out and greeted me and took me up to a higher floor where they had more of their engineers hiding.</p>
<p>The upper floor was still very nice and people were not too cramped at their desks.  There were not an cubicles, mostly open space.  Everyone had two monitors and either a PC or a Mac.  Some people had portable video conferencing devices so they could work with people from other offices with constant <a href="http://chase.ratchetsoftware.com/wp-content/2008/09/dog-mean.jpg" rel="lightbox[67]"><img style="float: right;" title="Mean Dog" src="http://chase.ratchetsoftware.com/wp-content/2008/09/dog-mean.jpg" alt="" width="100" height="100" /></a>communication.  Everyone I ran into seemed very nice.  It looked like the upper echelon of computer scientists.  That&#8217;s a weird way to put it, but most people reading this probably know what the lower portion of computer scientists consist of.  I&#8217;m talking social skills of course, not technical.  I was told that this office was becoming too small and they would soon be moving to a larger building.  Apparently, they would soon be allowed to bring dogs to work on one of the floors of the new building!  I always loved the thought of bringing my dogs to work with me instead of leaving them at home to be bored all day. After the quick walk through the upstairs office I was brought into this little room with a desk, a chair, and a whiteboard.</p>
<p>The interview began with a conversation between me and my recruiter who I had talked with on the phone one month prior.  We talked about what it was like to work at Google -- Boston.  He wanted to answer any sort of question I might have.  So I asked about all the various things their office did, the normal workday, the time commitment, etc.  Overall I got the feeling that the Boston office was more consistent in their eight hour workdays because the average age of employees was higher and they had families at home, etc.  I was very worried about all the articles I read about online about Google employees working to death without much compensation.  He then told me how the interview process was going to proceed.  I was going to be interviewed by four different engineers for 30 minutes each.  In the middle I was going to have lunch with another engineer.  With that he said his goodbyes and good lucks and introduced me to my first interviewer.</p>
<p>In order to avoid any problems, I&#8217;m only going to generically talk about the problems any of the engineers gave me.</p>
<p>The first engineer was a small asian guy who seemed very friendly.  He sat down across from me and asked a few questions about my research experience and tried to try to get a handle on some of my previous projects.  He then asked if I had any questions about Google for him and we moved on to the technical questions.  He had two questions for me.  Given an array of stock prices, what would be the best buy and sell point to make the most money.  I had read about problems similar to this while studying.  I had to modify it a bit in order to get the solution.  The similar problem I had studied was finding the largest sum of an array of positive and negative numbers.  For the stock question, I had to change each number to instead be the gain or loss for that day.  Then, the problem reduced to the largest sum of contiguous elements or <a href="http://en.wikipedia.org/wiki/Kadane%27s_Algorithm">Kadane&#8217;s algorithm</a>.  The second question was given a set of N terms, you have N linked lists each containing an IDs of a webpages that match the term the list corresponds to.  The important thing was that each list was in sorted order. I was to come up with a method to AND multiple terms together and retrieve the matching pages.  This was not similar to a problem I had studied and I had to come up with a couple ideas before settling on one.  The fact that they are in sorted order allows you to quickly determine if you need to continue a search or not in the second list.  The biggest challenge was when the interviewer then asked me to implement it.  I stumbled a bit with some exceptional cases and the fact that the board was so tiny.  We ran out of time before it was perfect, but I felt that it was sufficient.</p>
<p>The second engineer came in immediately after the first.  He was a larger bearded American guy. He also seemed nice, but much more focused and serious.  We started with the discussion about my experience just as the last engineer did, but we talked in depth about one of my networking research projects with sensor networks.  I was very surprised with how well the engineers seemed to have read my resume, they were very prepared and I appreciated that.  We then moved on to the questions.  The first one was tricky at first but I was able to get it without too many hints from the interviewer.  He was mostly giving positive sounds when I talked about something that was close to the right solution. The question was given a random number generator that generates the numbers between 1 and 7, how do you create a random number generator that generates 1 through 10 uniformly?  This was similar to questions I had read online about biased coin flipping.  You basically have to generate twice and take all the combinations that can occur, ie (1, 1), (1, 2), of which there should be 7*7=49.  Divide these sets evenly into 10 sets.  Then if you generate a combination that falls within one of these 10 sets, you have a number uniformly between 1 and 10.  There will be some sets that are left over, and if you get one of these you must regenerate your numbers.  He extended the question by asking what is the probability that you will have to regenerate your random numbers.  Then he further extended the question to ask if you could reuse the previous rolls so you only had to regenerate one random number.  So I&#8217;ll leave this one to you.  I then had to determine the probability of not getting a uniform number between 1 and 10 after 7 attempts or something like that.  His second question was much more discussion oriented.  He asked me to talk about how networking would be implemented in a simple board game.  He wanted to know how the data was sent over the network.  We focused on connect four because I had experience programming this game before.  We went into all the details of what each player must know about their opponents in order for them to play the game.  We then talked about what a player would need in order to be an observer, or to join a game in the middle, etc.  There wasn&#8217;t any tricks to this question, we just talked about game networking basically until our time ran out.</p>
<p>Finally, I was out of the first two interviews and they seemed to have gone relatively well.  I was now introduced to my third engineer and he was supposed to have lunch with me.  There are a lot of stories about the food services at Google.  I wasn&#8217;t sure what the setup would be in the Boston office, but I was hoping it was still free.  Sure enough, free lunches are the norm even at a smaller office (30 engineers) like this one.  It wasn&#8217;t one quite as big a scale as the Mountain View campus is, but it was a buffet of above average food brought down from a catering service on an upper floor of the same building.  I don&#8217;t remember what I had, but it was pretty healthy and tasty.  I was very excited about the giant glass doored fridge they had filled with all kinds of free sodas and water bottles.  I had a pretty tasty sparkling lemon drink that was covered with a foil top that you had to remove before opening it.  I don&#8217;t remember the name but I wish I did so I could get some.  The guy said they replaced the Sprite in the office with the lemon drink.  During lunch we talked a lot about the life at Google.  The guy had a lot to say about the people you get to work with, the types of projects and languages used, and his own experiences during the interview.  There were a couple other Googlers that sat down with us and joined in the discussion about life at Google.  Everyone seemed pretty happy while they enjoyed their free lunch (and free cake).  There were a bunch of board games in this room that looked like they were played when people were not eating.  It was a pretty small room for a cafeteria, like a large conference room that they placed lunch tables in.  I was pretty surprised to hear that you could bring friends and family to the lunch with you for free as long as you let them know they were coming ahead of time.  Another interesting thing I learned was that most of the engineers at this office were from MIT.  So much so that they all had stories of when they were in college together and partied together.  I had read previously that Google had a preference for Ivy league schools, and this kind of worried me about my own educational background.  After our talk we walked around the office for a more formal tour.  I saw most of the office the first go around because it was pretty small, but they said they would be moving pretty soon to a bigger location.  We ended back at the small office where I would continue the last two technical interviews.</p>
<p>I don&#8217;t know how to describe the next engineer except as spunky.  He was very happy and excited to be working there.  He explained to me that every Google employee spends about three months or so at the main campus in Mountain View, CA for training.  They said it&#8217;s a pretty crazy experience unlike anything else.  So we started back with the standard talk about my experience and moved into the technical questions.   I&#8217;m sorry but I can only remember one of the questions posed by this interviewer.  He asked me to describe all the details involved in implementing something similar to the wireless access points at the airports.  When you first log on it redirects your browser to a page that forces you to either pay or sign in.  We talked about various security concerns and problems with the current approach.  He then asked me a question I did on the white board but can&#8217;t remember it for the life of me.  It might have been the stock quote question that I thought was in the first interview.  It really doesn&#8217;t matter though as I think all the questions I was asked are in this post somewhere.</p>
<p>The last interviewer was another large bearded American guy.  We again talked about my experience and any questions I might have about Google.  The questions posed by this engineer were the ones that stumped me the most.  One was very simple but I was too quick to come up with a solution and it had some flaws.  The question was given a binary tree, determine if it is a binary search tree.  This is a typical problem and I should have had an immediate answer.  My answer did not work on a special case and he didn&#8217;t tell me this before asking me to implement it.  I then ran into trouble while implementing it and we talked about why and figured out what my problem was.  This interviewer didn&#8217;t seem near as excited or happy to be interviewing me as the previous engineers.  The second question he asked was an event scheduling problem.  Basically you were given events with a start time and an end time.  You were then to determine if there were any conflicts in any of the events.  He would then extend the question to find a O(n) solution.  I don&#8217;t think I came up with a solution to his satisfaction during the interview.  My solution involved a hashing technique so we could determine collisions between events if the same hash was found.  I&#8217;m sure there is a much better solution, but this was at the end of the day and the guy was not being very helpful with hints.  I  felt the worst about this interview and so I left on a bad note.</p>
<p>I was then taken downstairs by another engineer and we made sure everything was in order and he said goodbye.  I felt kind of rushed out the door without any kind of thank you for coming or anything like that.  I felt good about most of the interview, but the last interview would bother me for the next few weeks.</p>
<h3>Aftermath</h3>
<p>So I was finally back home and couldn&#8217;t stop worrying about the results of the interview.  I waited a few weeks but didn&#8217;t hear anything back.  I had sent a thank you email a couple days after the email but I didn&#8217;t get any reply.  After about three weeks my secondary phone rang (again I don&#8217;t know why they have this number).  It the recruiter from the Boston office and he was letting me know that I didn&#8217;t get the job.  He said my background was not matching what they needed at that time.  I thought this was a very odd way of putting it.  This made me think that the rumors about them preferring certain colleges were at least partially true.  I&#8217;ll never know the real reasons why, but I did feel that I did pretty well during most of my interview.</p>
<p>Since then, I&#8217;ve come to terms that I might not be a good fit for a company like Google.  I feel like I should be putting my talents to actually create something useful and start my own company.  I know it&#8217;s a common dream, but I feel like I have a few good ideas and I&#8217;m working to make them a reality.  So, I haven&#8217;t applied to any new companies since then because I&#8217;ve been doing Ph.D. work and working on my own company Ratchet Software.</p>
<p>I hope that this story about my experience interviewing at Google will be of some sort of help to someone.  I know when I was interviewing I was looking for every resource I could find on the topic.  I had only found one blog post that mentioned anything about the Boston office.  I know there are a ton of people trying to get their foot in the door at Google.  I just hope that my story helps them do that.  Good luck to all the future Googlers out there.  I own some Google stock and would love to see some of you young motivated engineers come up with the next Gmail.   <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_lol.gif' alt=':lol:' class='wp-smiley' /> </p>
<p>- Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/09/drinking-the-kool-aid/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Necklace Simulation</title>
		<link>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/</link>
		<comments>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 19:51:28 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=54</guid>
		<description><![CDATA[I had some ideas that required some sort of necklace simulator through a web interface.  I didn&#8217;t have much experience programming anything complex in ActionScript, but I was sure that would be the way to go.  I have seen some amazing work done with Flash applications over the past few years and I had no [...]]]></description>
			<content:encoded><![CDATA[<p>I had some ideas that required some sort of necklace simulator through a web interface.  I didn&#8217;t have much experience programming anything complex in ActionScript, but I was sure that would be the way to go.  I have seen some amazing work done with Flash applications over the past few years and I had no doubt that Flash could handle such an application.  So I set out on one weekend to put together a quick prototype for my idea.</p>
<p><span id="more-54"></span></p>
<p>Of course the first question I had was what physics libraries are available for Flash these days.  I had some experience in the past using Newton and various wrappers in C++ for a game project.  My experience with Newton would end up helping me pick up a new physics engine relatively easy.</p>
<p>There were a couple of choices to choose from and I started my search by reading some reviews found on <a title="Geek Glue Actionscript Physics Engine Overview" href="http://http://geekglue.blogspot.com/2008/01/actionscript-2d-physics-engines.html" target="_blank">GeekGlue.</a> From this I was able to reduce my search to the <a title="APE" href="http://www.cove.org/ape/index.htm" target="_blank">Actionscript Physics Engine (APE)</a> and<a title="Box2D Flash ActionScript 3" href="http://box2dflash.sourceforge.net/" target="_blank"> Box2DFlashAS3</a>.  APE seemed much easier to get started with, but I wasn&#8217;t sure if it would accomplish what I wanted without a lot of massaging.  After further reading on some forums, I found that the rope simulation in APE is much more limited because there is only one type of joint available at this time.  Box2D has 6 different types of joints and their provided rope bridge looked a little better.  The fact that Box2D had ragdoll physics examples made me feel that I would have as much control as I needed to create the necklace simulation I wanted.</p>
<p>After choosing Box2D I wanted to take their rope &#8220;springy&#8221; bridge demo and see if that could be easily turned into some sort of free falling rope.  This was not as simple as adding links and changing a few variables like I had hoped.  If you simply add links to the rope, after adding too many the iterative solver breaks the joints with the default configuration.  I had to search for a while to figure out why this was.  After some searching, I found that a joint can only handle about a 10:1 weight ratio before giving out.  The solution to this is the increase the number of iterations of decrease the step time.  This of course will slow down your simulation depending on how much you have to increase the number of iterations.  I had to change mine from about 10 to 40.  My simulation was still running at a decent speed but in the future I am not sure how this will hold up with more physics bodies flying around.  This was a huge problem for a while for me and I was very glad to finally find a solution.  If you have joints that are breaking due to too much force being applied to them is too much without adding more iterations to the engine so it can accurately solve the joint forces.  I hope someone with a similar problem can find this post because it took me a while to find a solution on a forum somewhere.</p>
<p>I am not concerned with creating a nice looking application at this time since this was just a weekend project I was doing on the side.  The goals for the prototype of my necklace simulator I wanted to have  the following:</p>
<ol>
<li>A rope like necklace that could be moved around by the user with realistic physics.</li>
<li>Two end pieces on the necklace that were different than the rest of the necklace and could be connected to things.</li>
<li>Two or more connectors in the world that could have a necklace endpiece connected to them and unattached when the user wants to move the necklace again.</li>
<li>A prototype of a bead which can be moved by the user and placed on the necklace.</li>
<li>A background of a neck or something so that it is easy to see what the necklace would look like on a person.</li>
</ol>
<p>For my resulting application, I simply modified the default Test class provided with Box2DFlashAS3 to have instances of the objects in the world such as the rope and beads.  Previously it was a base class for various tests.  This might not be the best solution, but it was fine for my prototype to see if things would work.  I&#8217;ll include the entire source here and highlight my changes as well.</p>
<p><strong>Playground.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">package</span> TestBed<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Main;
	<span style="color: #0033ff; font-weight: bold;">import</span> General.Input;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getTimer</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Playground<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Playground<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			m_sprite = Main.m_sprite;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1000.0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1000.0</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1000.0</span>, <span style="color: #000000; font-weight:bold;">1000.0</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Define the gravity vector</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> gravity<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.0</span>, <span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Allow bodies to sleep</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> doSleep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
&nbsp;
			<span style="color: #009900;">// Construct a world object</span>
			m_world = <span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB, gravity, doSleep<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// set debug draw</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//var dbgSprite:Sprite = new Sprite();</span>
			<span style="color: #009900;">//m_sprite.addChild(dbgSprite);</span>
			dbgDraw.m_sprite = m_sprite;
			dbgDraw.m_drawScale = <span style="color: #000000; font-weight:bold;">30.0</span>;
			dbgDraw.m_fillAlpha = <span style="color: #000000; font-weight:bold;">0.3</span>;
			dbgDraw.m_lineThickness = <span style="color: #000000; font-weight:bold;">1.0</span>;
			dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit <span style="color: #000000; font-weight: bold;">|</span> b2DebugDraw.e_jointBit;
			m_world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Create border of boxes</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> wallSd<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> wallBd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> wallB<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
			<span style="color: #009900;">// Left</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">95</span> <span style="color: #000000; font-weight: bold;">/</span> m_physScale, <span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallSd.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// Right</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> m_physScale, <span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// Top</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			wallSd.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">680</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//wallB = m_world.CreateBody(wallBd);</span>
			<span style="color: #009900;">//wallB.CreateShape(wallSd);</span>
			<span style="color: #009900;">//wallB.SetMassFromShapes();</span>
			<span style="color: #009900;">// Bottom</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			m_rope = <span style="color: #0033ff; font-weight: bold;">new</span> Rope<span style="color: #000000;">&#40;</span>m_world, m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
			m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
			m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
			m_leftHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">175</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			m_rightHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			m_hookJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			m_bead = <span style="color: #0033ff; font-weight: bold;">new</span> Bead<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #000000; font-weight:bold;">7.5</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #009900;">// Update mouse joint</span>
			UpdateMouseWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			MouseDestroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Update physics</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> physStart<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_world.Step<span style="color: #000000;">&#40;</span>m_timeStep, m_iterations<span style="color: #000000;">&#41;</span>;
			Main.m_fpsCounter.updatePhys<span style="color: #000000;">&#40;</span>physStart<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Member Data</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_world<span style="color: #000000; font-weight: bold;">:</span>b2World;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_mouseJoint<span style="color: #000000; font-weight: bold;">:</span>b2MouseJoint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_iterations<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">40</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_timeStep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// world mouse position</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorldPhys<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorldPhys<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorld<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorld<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #009900;">// Sprite to draw in to</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rope<span style="color: #000000; font-weight: bold;">:</span>Rope;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_hookJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_bead<span style="color: #000000; font-weight: bold;">:</span>Bead;
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Update mouseWorld</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> UpdateMouseWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			mouseXWorldPhys = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale;
			mouseYWorldPhys = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale; 
&nbsp;
			mouseXWorld = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span>;
			mouseYWorld = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Mouse Drag</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// mouse press</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #000000; font-weight: bold;">!</span>m_mouseJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> md<span style="color: #000000; font-weight: bold;">:</span>b2MouseJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2MouseJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.body1 = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.body2 = body;
					md.<span style="color: #004993;">target</span>.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
					md.maxForce = <span style="color: #000000; font-weight:bold;">600.0</span> <span style="color: #000000; font-weight: bold;">*</span> body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.timeStep = m_timeStep;
					m_mouseJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>md<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2MouseJoint;
					body.WakeUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_rope.m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rope.m_rightHook<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_leftHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
								m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_rightHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
								m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_bead.m_body<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">// mouse release</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">//get body being clicked on</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> clickedBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_mouseJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>;
					m_mouseJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>clickedBody == m_bead.m_body<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rope.IsAttachable<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
&nbsp;
						<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHook.IsTouching<span style="color: #000000;">&#40;</span>clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;test&quot;</span><span style="color: #000000;">&#41;</span>;
							m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_leftHook.m_body,  clickedBody, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
							<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
								m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
							<span style="color: #000000;">&#125;</span>
							m_leftHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHook.IsTouching<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
								m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
							<span style="color: #000000;">&#125;</span>
							m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_rightHook.m_body,  clickedBody,  <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
							m_rightHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">// mouse move</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> p2<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
				m_mouseJoint.SetTarget<span style="color: #000000;">&#40;</span>p2<span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Mouse Destroy</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDestroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// mouse press</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; Input.isKeyPressed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">68</span><span style="color: #3f5fbf;">/*D*/</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					m_world.DestroyBody<span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">return</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// GetBodyAtMouse</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> mousePVec<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetBodyAtMouse<span style="color: #000000;">&#40;</span>includeStatic<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// Make a small box.</span>
			mousePVec.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> aabb<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			aabb.lowerBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			aabb.upperBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Query the world for overlapping shapes.</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> k_maxCount<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">10</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> shapes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = m_world.Query<span style="color: #000000;">&#40;</span>aabb, shapes, k_maxCount<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = <span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; count; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.IsStatic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> == <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000; font-weight: bold;">||</span> includeStatic<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape = shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2Shape;
					<span style="color: #6699cc; font-weight: bold;">var</span> inside<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = tShape.TestPoint<span style="color: #000000;">&#40;</span>tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetXForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, mousePVec<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>inside<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						body = tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> body;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The first change is at the end of the constructor where I create the items that are in the world.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">m_rope = <span style="color: #0033ff; font-weight: bold;">new</span> Rope<span style="color: #000000;">&#40;</span>m_world, m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
m_leftHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">175</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
m_rightHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
m_hookJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
m_bead = <span style="color: #0033ff; font-weight: bold;">new</span> Bead<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #000000; font-weight:bold;">7.5</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>The second noticable difference is the addition of the member variables.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rope<span style="color: #000000; font-weight: bold;">:</span>Rope;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_hookJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_bead<span style="color: #000000; font-weight: bold;">:</span>Bead;</pre></div></div>

<p>In the mouse drag function are the most important additions.  In the first part of this function we are focusing on mouse presses, or an initiated drag.  The first thing we do is check to see if we clicked on a joint at one of our hooking points, if so we want to destroy that joint and let the mouse move the necklace wherever it wants to.  We also want the user to be able to click a bead and place it on the necklace without colliding with the necklace while trying to drag the bead, this is also check for here, we turn off collisions for a bead if it is being dragged.</p>
<p>\The second part of the mouse drag function is when the mouse button is relased after dragging has occurred.  When this happens, we check to see if we were holding a bead, if we were we set it to be collidable, so that it will stay on the necklace if it was placed there.  We are also checking to see if the right or left hook are clicked on with one of the end pieces of the necklace.  If they are then we create a new joint between the hook and the necklace endpiece.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//======================</span>
<span style="color: #009900;">//======================</span>
<span style="color: #009900;">// Mouse Drag</span>
<span style="color: #009900;">//======================</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// mouse press</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #000000; font-weight: bold;">!</span>m_mouseJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> md<span style="color: #000000; font-weight: bold;">:</span>b2MouseJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2MouseJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.body1 = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.body2 = body;
			md.<span style="color: #004993;">target</span>.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			md.maxForce = <span style="color: #000000; font-weight:bold;">600.0</span> <span style="color: #000000; font-weight: bold;">*</span> body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.timeStep = m_timeStep;
			m_mouseJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>md<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2MouseJoint;
			body.WakeUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_rope.m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rope.m_rightHook<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_leftHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
						m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_rightHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
						m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_bead.m_body<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">// mouse release</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//get body being clicked on</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> clickedBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_mouseJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>;
			m_mouseJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>clickedBody == m_bead.m_body<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rope.IsAttachable<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHook.IsTouching<span style="color: #000000;">&#40;</span>clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;test&quot;</span><span style="color: #000000;">&#41;</span>;
					m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_leftHook.m_body,  clickedBody, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
						m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #000000;">&#125;</span>
					m_leftHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHook.IsTouching<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
						m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #000000;">&#125;</span>
					m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_rightHook.m_body,  clickedBody,  <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
					m_rightHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">// mouse move</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> p2<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
		m_mouseJoint.SetTarget<span style="color: #000000;">&#40;</span>p2<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There are three types that are used in the Playground.as file that we haven&#8217;t seen and are not in Box2D.  Those are the Rope, Bead, and Hook classes.</p>
<p>The rope is the most complicated and has the most new code.  When instantiated it builds itself vertically at a hard coded location.  It has an endpiece on each end that is different than the rest of the necklace.   Initially I made all pieces of the necklace a rectangle and connected them end to end.  This proved to be a mistake because when there was too much bending or twisting between two rectangle shapes at a joint they would sometimes not know how to fix themselves.  It would look like a permanent crease between the joint.  I was unable to figure out how to fix this problem with rectangles and I figured collisions between spheres is always cheaper so I moved everything to circles.</p>
<p>So the result is that the rope is made up of anywhere between 10 and 60 circles.  The values for the number of circles and the size of them is currently hard coded in the rope.as file.  Most of what is happening during the building of the rope is pretty obvious.  I changed a couple of the collision properties to make things look more realistic.</p>
<p>Each end piece is special and can be attached to a connector in the stage.  The isAttachable function checks to see if a given body can be attached to something.  I did this so it was easy to change which things could be attached in the future.</p>
<p>One last thing to note is that I made the end pieces collidable with the necklace pieces but the necklace pieces are not collidable with themselves.  This saves a lot of computation I&#8217;m sure but the main reason was so that you could more easily find an endpiece among a pile of necklace since it should be sitting on top.</p>
<p><strong>Rope.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rope<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Rope<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #009900;">// Set block length &amp;amp; width &amp;amp; overlap(to make block connections look smoother)</span>
		m_blockLength =<span style="color: #000000; font-weight:bold;">3.5</span>;
		m_blockWidth = <span style="color: #000000; font-weight:bold;">3.5</span>;
		m_overlap = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">4.5</span>;
		<span style="color: #009900;">// Set end piece paramaters</span>
		m_circleRadius = <span style="color: #000000; font-weight:bold;">3</span>;
&nbsp;
		<span style="color: #009900;">// Starting height for necklace</span>
		m_startingHeight = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">200</span>; 
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> vel<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		vel.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> anchor<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
		<span style="color: #009900;">// Bridge</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> sd<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			sd.SetAsBox<span style="color: #000000;">&#40;</span>m_blockLength <span style="color: #000000; font-weight: bold;">/</span> physScale, m_blockWidth <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			sd.density = <span style="color: #000000; font-weight:bold;">20</span>;
			sd.friction = .8;
			sd.restitution = <span style="color: #000000; font-weight:bold;">0.0</span>;
			sd.<span style="color: #004993;">filter</span>.groupIndex = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			cd.density = <span style="color: #000000; font-weight:bold;">20.0</span>;
			cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
			cd.restitution = <span style="color: #000000; font-weight:bold;">0.0</span>;
			cd.friction=<span style="color: #000000; font-weight:bold;">0.8</span>;
			cd.<span style="color: #004993;">filter</span>.groupIndex = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">3</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bd.<span style="color: #004993;">angle</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">90</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> jd<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			const numPlanks<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">50</span>;
&nbsp;
			jd.lowerAngle = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			jd.upperAngle = <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			jd.enableLimit = <span style="color: #0033ff; font-weight: bold;">false</span>;
			jd.collideConnected = <span style="color: #0033ff; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> prevBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = ground;
&nbsp;
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>physScale<span style="color: #000000;">&#41;</span>
			<span style="color: #009900;">//bd.isBullet = true;</span>
			body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>sd<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			body.SetLinearVelocity<span style="color: #000000;">&#40;</span>vel<span style="color: #000000;">&#41;</span>;
			m_leftHook = prevBody = body;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; numPlanks; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
				bd.isBullet = <span style="color: #0033ff; font-weight: bold;">false</span>;
				bd.linearDamping = <span style="color: #000000; font-weight:bold;">0.1</span>;
				bd.angularDamping = <span style="color: #000000; font-weight:bold;">0.5</span>;
				body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
				body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
				body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				anchor.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
				jd.Initialize<span style="color: #000000;">&#40;</span>prevBody, body, anchor<span style="color: #000000;">&#41;</span>;
&nbsp;
				world.CreateJoint<span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span>;
&nbsp;
				prevBody = body;
			<span style="color: #000000;">&#125;</span>
			bd.linearDamping = <span style="color: #000000; font-weight:bold;">0</span>;
			bd.angularDamping = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #009900;">//bd.isBullet = true;</span>
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength <span style="color: #000000; font-weight: bold;">+</span> m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>numPlanks <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>sd<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_rightHook = body;
			anchor.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			jd.Initialize<span style="color: #000000;">&#40;</span>prevBody, body, anchor<span style="color: #000000;">&#41;</span>;
			world.CreateJoint<span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> IsAttachable<span style="color: #000000;">&#40;</span>body<span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rightHook<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_blockLength<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_blockWidth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_overlap<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_startingHeight<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>b2Body;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Creating a bead class that could be placed on the necklace was one thing I was unsure of how to do initially.  The end result for my prototype was to create three circle shapes on a single body and to make the center one a sensor.  A sensor is something that can collide with other objects but doesn&#8217;t cause any actual physical response.  It is essentially not collidable but still keeps track of its collisions.</p>
<p>So the constructor creates this complex body.  I found very little documentation on how to make a simple complex body out of simple shapes online.  It was very easy though and if you look at the bead.as constructor you can see how I did it.  The most confusing part was how I was supposed to offset the different shapes in the body.  This is accomplished via the Shape class&#8217; localPosition attribute.  By modifying this attribute I could offset the shape in the body it will eventually be a part of.</p>
<p>The last thing in this class is the setCollidable function.  I needed a way to easily make all of the parts of the bead a sensor temporarily.  This was so that the user could drag the bead and not collide with anything and then let go and it would become collidable again.<br />
<strong>Bead.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Bead<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Bead<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		m_circleRadius = radius;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		cd.density = <span style="color: #000000; font-weight:bold;">0.01</span>;
		cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span>;
		cd.friction = <span style="color: #000000; font-weight:bold;">0.1</span>;
		cd.restitution = <span style="color: #000000; font-weight:bold;">0.1</span>;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		cd.localPosition.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
&nbsp;
		bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">/</span>physScale, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span>physScale<span style="color: #000000;">&#41;</span>
		bd.isBullet = <span style="color: #0033ff; font-weight: bold;">true</span>;
		m_body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
		m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		cd.localPosition.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">15</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
		m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		cd.localPosition.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		cd.isSensor = <span style="color: #0033ff; font-weight: bold;">true</span>;
		m_centerShape = m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		m_body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SetCollidable<span style="color: #000000;">&#40;</span>toggle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> currentShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape = m_body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>currentShape; currentShape; currentShape = currentShape.m_next<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>currentShape == m_centerShape<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">continue</span>;
			<span style="color: #000000;">&#125;</span>
			currentShape.m_isSensor = <span style="color: #000000; font-weight: bold;">!</span>toggle;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_position<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_centerShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The hook class is very simple and just creates a circle in the location where it is supposed to be.  I temporarily just used the physics circle to make it easy to test.  I don&#8217;t plan to have a physics body be the connector piece in the future unless I am using so sort of callback function.  I think it is easy enough to use the isTouching function to check if a body is within touching range of this hook.<br />
<strong>Hook.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Hook<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Hook<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,  <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span>b2Vec2, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		m_circleRadius = radius;
		m_position = <span style="color: #004993;">position</span>;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			cd.density = <span style="color: #000000; font-weight:bold;">1.0</span>;
			cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span>;
			cd.isSensor = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>m_position.<span style="color: #004993;">x</span>, m_position.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>
			m_body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> IsTouching<span style="color: #000000;">&#40;</span>body<span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span> = body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.Copy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #004993;">position</span>.Subtract<span style="color: #000000;">&#40;</span>m_position<span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">position</span>.Abs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot; x units away.&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;radius of circle is &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> m_circleRadius<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&amp;</span>lt; m_circleRadius <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #004993;">position</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&amp;</span>lt; m_circleRadius<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_position<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The resulting application turned out pretty good.  I was not totally satisfied with placing a bead on the necklace, but I ran out of time and had to do real work by that time.  I think I could make the bead automatically orient correctly and highlight itself when it is being placed on the right spot on the necklace.  Besides that problem I am satisfied with the it as a prototype for a future application I might develop.</p>
<p>Here it is:</p>
<p><strong>Beadr Prototype 1:</strong></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_beadr_1990057623"
			class="flashmovie"
			width="600"
			height="400">
	<param name="movie" value="http://chase.ratchetsoftware.com/media/beadr.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://chase.ratchetsoftware.com/media/beadr.swf"
			name="fm_beadr_1990057623"
			width="600"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Technical Background</title>
		<link>http://chase.ratchetsoftware.com/2008/08/technical-background/</link>
		<comments>http://chase.ratchetsoftware.com/2008/08/technical-background/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 00:07:13 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[school]]></category>
		<category><![CDATA[business]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=5</guid>
		<description><![CDATA[I&#8217;ve decided it will be good for me if I can sit down once a week and try to write about a problem I had to solve that week, either in my daily life or working on a project.
The biggest problem for this week was actually making myself sit down and write something on here.  [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided it will be good for me if I can sit down once a week and try to write about a problem I had to solve that week, either in my daily life or working on a project.</p>
<p>The biggest problem for this week was actually making myself sit down and write something on here.  I don&#8217;t think that would be very interesting to readers or my future self so I&#8217;ll fore go the discussion of that.</p>
<p>I think the first thing I should do is explain my background.  I am a Ph.D. candidate at the University of South Carolina (USC) working in the ARENA for Research on Emerging Networks and Applications (<a title="ARENA" href="http://arena.cse.sc.edu" target="_blank">ARENA)</a> lab.  Here I focus on developing new wireless network protocols in an endless quest to publish papers.  I have been with Dr. Srihari Nelakuditi since about 2004, when he hired me as an undergraduate researcher.  We have presented multiple papers together.  One was presented at MobiCom and can be found in the Mobile Computer Communications Review (<a title="Pair-wise resistance to traffic analysis in MANETs" href="http://portal.acm.org/citation.cfm?id=1374512.1374518&amp;coll=ACM&amp;dl=ACM&amp;idx=J548&amp;part=newsletter&amp;WantType=Newsletters&amp;title=ACM%20SIGMOBILE%20Mobile%20Computing%20and%20Communications%20Review" target="_blank">MCCR</a>), it is on security in Mobile Ad Hoc NETworks (MANETs).  Another notable paper was presented at the WiMesh workshop 2008 at SECON, it is not yet available online and focused on bit-rate selection in oppotunistic routing.  The second paper was also the focus of my Master&#8217;s Thesis.</p>
<p><span id="more-5"></span></p>
<p>During my time as a student at USC, I have worked on quite a few side projects.  Some of these were related to school and some could be seen as distractions.  I spent some time doing contract work on a health-care data system for a director of marketing of a contract company for nursing homes.  I have since used my experience in this field to try to create a large scale public web application providing similar information.  The project is being developed in Ruby on Rails, my first large scale project using the framework.  I have been very happy with it for the most part, and I hope that I can provide some useful insight into problems I have faced while programming with it.</p>
<p>During this Summer of 2008, though, I took a break and had a tour at a nameless agency in the Maryland area doing computer science research <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .   This was very fun, but nonetheless the day to day government work motivated me even further to pursue my goal of having my own business.  I know (and am told very frequently) that this is a pipe dream and every young computer scientist wants to have their own company, but I feel like I have the motivation and drive to bring a reasonable idea to fruition.  If the completion of my plans does not bring any type of success then I will simply move on.  The key point is that I do plan to have an operational business before deciding that it is not successful.  I believe this is the reason most technical start-ups fail.  They don&#8217;t realize how much work is involved in creating their envisioned product.</p>
<p>I have also spent some time while at school developing games.  See, this was my first love with computers.  I fully expected to be a video game developer by the time I graduated college.  There are multiple things that changed my mind about this goal.  The first is that I feel like the industry has totally changed.  This might be due to many reasons.  My observations lead me to believe it is a combination of the following</p>
<ol>
<li>The large emerging market of the casual gamer, usually on a console.</li>
<li>Companies like EA buying up every company they can and losing all creativity in their games for the sake of optimization.</li>
<li>The failing PC market for games, which prevents the small developers from getting a market unless they can get their game on one of the major consoles.</li>
</ol>
<p>So, I feel like the video game market is no longer somewhere I would like to work, besides a select few companies.  The competition for getting into the industry is enormous.  I feel like I could find a very enjoyable job in normal software development that pays much better, has less stress, and does not require me to do a little dance for the hiring guys culling through hundreds of potential code monkeys.  Regardless, I still took a game development class this past semester.  Despite having a five person team, it ended up giving me much needed experience with slack teammates.  I&#8217;ve had slack teammates before, but most of the time it was because they were either incapable of creating decent code, or they just didn&#8217;t care about school in general.  This graduate class was different though, in that my teammates were perfectly capable of accomplishing what I tasked them with.  I believe that the problem was that they saw that I was also capable of doing it myself, so any work they didn&#8217;t finish was not going to cause them to get a bad grade.  The class ended well, our game turned out OK, but the team was very unsatisfied with themselves for being so uninvolved during the semester.  I&#8217;m generalizing about their lack of work, not every team member was horrible, I can think of two that I would have given F&#8217;s to though.</p>
<p>If you&#8217;ve read this far, you might have noticed something about the way I write and think.  I move very quickly from one topic to the next.  I often find myself talking and something comes up in my head and it seems like it would be more time efficient to talk about that instead so I simply move to that topic without a very smooth transition.  I&#8217;m afraid if I simply free write that is how it will turn out.  If I&#8217;m writing a technical article, I can rewrite it as many times as I need to.  So there will probably be two different types of writing on this blog.</p>
<p>Thank you for reading.  In my next post I am hoping to go over some problems I have faced in Rails and my current solutions to the problems.  Hopefully I can get some feedback and maybe make some improvements.</p>
<p>-Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/08/technical-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
