<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>A recovering product manager rediscovers code.</description><title>David on Code</title><generator>Tumblr (3.0; @davidoncode)</generator><link>http://davidoncode.tumblr.com/</link><item><title>detecting iphone/ipad in rubymotion</title><description>&lt;p&gt;I just found this post in my drafts pile; might be a little old by now, but here goes.&lt;/p&gt;

&lt;p&gt;Most standard texts on iOS programming say to use the following code to detect whether you&amp;#8217;re running on an iPhone or an iPad:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Well, that won&amp;#8217;t work in RubyMotion because &lt;code&gt;UI_USER_INTERFACE_IDIOM&lt;/code&gt; is a C macro - meaning that it gets handled in the preprocessor and never gets turned into a symbol in the compiled libraries. That&amp;#8217;s necessary for the RubyMotion build system to see it and create the proper Ruby objects to mirror it.&lt;/p&gt;

&lt;p&gt;But, if you dig down into the C header files deep enough, you find:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/* The UI_USER_INTERFACE_IDIOM() macro is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.
 */&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]&amp;#160;? [[UIDevice currentDevice] userInterfaceIdiom]&amp;#160;: UIUserInterfaceIdiomPhone)&lt;code&gt;&lt;/code&gt;&lt;/h1&gt;

&lt;p&gt;The macro&amp;#8217;s only needed for iOS version 3.2 and earlier - and really, why bother?&lt;/p&gt;

&lt;p&gt;So, the Ruby code that works for this is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;    if UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
      storyboard = UIStoryboard.storyboardWithName "iPad-Storyboard", bundle: NSBundle.mainBundle
    else
      storyboard = UIStoryboard.storyboardWithName "iPhone-Storyboard", bundle: NSBundle.mainBundle
    end&lt;/code&gt;&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/41223769840</link><guid>http://davidoncode.tumblr.com/post/41223769840</guid><pubDate>Tue, 22 Jan 2013 17:13:07 -0500</pubDate><category>ios</category><category>rubymotion</category></item><item><title>AVAudioPlayer and you</title><description>&lt;p&gt;I&amp;#8217;ve changed direction from Rails into iOS programming now&amp;#8230; And ran into a big frustration point.&lt;/p&gt;

&lt;p&gt;The app I&amp;#8217;m working on needs to record and play back audio. So into AVFoundation I go!&lt;/p&gt;

&lt;p&gt;The API annoys me. There are a few methods on &lt;code&gt;AVAudioPlayer&lt;/code&gt; which can return false in case of error - but there&amp;#8217;s no way to tell what the error is. So you have to jerk around experimenting for a while until you figure it out.&lt;/p&gt;

&lt;p&gt;Gee. Thanks, Apple.&lt;/p&gt;

&lt;p&gt;In my particular case, the &lt;code&gt;play&lt;/code&gt; method was returning false; this seems to be because I had not set the audio session category correctly. I&amp;#8217;ve yet to find somewhere in the Apple API documentation where it talks about the consequences of having your &lt;code&gt;AVAudioSession&lt;/code&gt; set up incorrectly.&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/40878615031</link><guid>http://davidoncode.tumblr.com/post/40878615031</guid><pubDate>Fri, 18 Jan 2013 19:02:32 -0500</pubDate><category>iOS</category><category>AVFoundation</category></item><item><title>MyFairElection</title><description>&lt;p&gt;Since we last spoke, my good friend &lt;a href="http://twitter.com/smoodle"&gt;Karen Suhaka&lt;/a&gt; convinced me to come on a bit of a mad dash with her&amp;#8230;&lt;/p&gt;

&lt;p&gt;The idea is called MyFairElection, and the &amp;#8220;X for Y&amp;#8221; version of the description is &amp;#8220;Yelp for polling places&amp;#8221;.&lt;/p&gt;

&lt;p&gt;So to be successful - we need to build a site that&amp;#8217;s usable and robust, than can deal with a number of weird corner cases in how states and jurisdictions deal with voting, and that can scale up to thousands of polling places and potentially millions of users.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s just a little bit audacious, and has a hard deadline fixed by law.&lt;/p&gt;

&lt;p&gt;We&amp;#8217;re in the initial building stages now. The decision we need to make in the next 2 weeks will be whether we think we can make it; if not, the backoff is to have it ready for a smaller election sometime in 2013, with the aim of building the brand for the big national elections in 2014 and 2016.&lt;/p&gt;

&lt;p&gt;In terms of technology - starting with Rails and MySQL, with hosting provided by Rackspace. Trying to be uber-BDD - but then I just threw Devise into the thing without writing a single test. The adventure will continue&amp;#8230;&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/32317084079</link><guid>http://davidoncode.tumblr.com/post/32317084079</guid><pubDate>Wed, 26 Sep 2012 02:22:15 -0400</pubDate></item><item><title>debugging FTW!</title><description>&lt;p&gt;RubyMotion added a debugger today. Hooray!&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/31949569462</link><guid>http://davidoncode.tumblr.com/post/31949569462</guid><pubDate>Thu, 20 Sep 2012 19:26:42 -0400</pubDate></item><item><title>Grr ruby local variables shadow setters!</title><description>&lt;p&gt;Time for a Ruby setter/getter pair!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def bar
  @bar
end

def bar=(b)
  @bar = b
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Seems workable enough, right?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1.9.3p194 :008 &amp;gt; bar
 =&amp;gt; nil 
1.9.3p194 :009 &amp;gt; bar=100
 =&amp;gt; 100 
1.9.3p194 :010 &amp;gt; bar
 =&amp;gt; 100 
1.9.3p194 :011 &amp;gt; self.bar
 =&amp;gt; nil 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Uh. Huh?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1.9.3p194 :014 &amp;gt; self.bar=200
 =&amp;gt; 200 
1.9.3p194 :015 &amp;gt; self.bar
 =&amp;gt; 200 
1.9.3p194 :016 &amp;gt; bar
 =&amp;gt; 100 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wha?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1.9.3p194 :017 &amp;gt; local_variables
 =&amp;gt; [:bar, :_] 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Oh. If you try to use the setter unadorned by an object - like, say, in another method in the same object - you end up making a local variable instead. I&amp;#8217;m sure there&amp;#8217;s a good reason for this, but I really cannot think of what it possibly could be right now. Sent me down a rathole for about 4 hours.&lt;/p&gt;

&lt;p&gt;This has the neat side effect of making it impossible for &amp;#8220;foo=&amp;#8221; style methods to be private. So if you want to make your setter private for some reason - like say the getter is read-only to other objects - you need to name it something else. Meh.&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/27494789639</link><guid>http://davidoncode.tumblr.com/post/27494789639</guid><pubDate>Wed, 18 Jul 2012 14:28:13 -0400</pubDate></item><item><title>Mac OS X users, upgrade your ruby!</title><description>&lt;p&gt;If you are running:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Mac OS X Lion 10.7.4&lt;/li&gt;
&lt;li&gt;Xcode 4.3.3 installed via the App Store&lt;/li&gt;
&lt;li&gt;ruby 1.9.3&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&amp;#8230;and you&amp;#8217;ve removed previous versions of the Xcode environment, when you build gems with native extensions, you may see this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/Users/dpassage/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb
creating Makefile

make
compiling autolink.c
make: /usr/bin/gcc-4.2: No such file or directory
make: *** [autolink.o] Error 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means you need to upgade your ruby!  ruby 1.9.3-p125 contains a patch which hardcodes the gcc version used on OS X as gcc-4.2.  The newer version fixes this, and just uses /usr/bin/gcc.  As it should.&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/27133570826</link><guid>http://davidoncode.tumblr.com/post/27133570826</guid><pubDate>Fri, 13 Jul 2012 13:39:14 -0400</pubDate></item><item><title>Rails and Referential Integrity</title><description>&lt;p&gt;So in the course of working on that new Rails-based web site for our silly obsessive games nights, I ran into a problem:&lt;/p&gt;
&lt;p&gt;In an effort to maintain the data from the old, PHP-based app, I&amp;#8217;m starting with the original database schema and using Rails migrations to bash it into shape, adapting it to the Rails conventions and the like.&lt;/p&gt;
&lt;p&gt;Anyway, things are going along well, but I ran into a problem: The database has no referential integrity.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example. I have a table of Drivers, a table of Races, and a &amp;#8220;join&amp;#8221; table called RaceEntries, where row has a pointer to a Driver and to a Race.  But there was nothing in the database, or in the code, which would prevent someone from deleting a Driver who was already entered into a Race. When that happens, you can get unexpected results when doing things like computing which driver won the most races.&lt;/p&gt;
&lt;p&gt;This wasn&amp;#8217;t really a problem when I was the only one updating results on the site, and I could go in directly with the mysql command line and fix things. (OK, actually, that was a problem, but one I could live with.) But given that one of the goals of recoding the site in Rails is to allow others to enter results, I needed it to be more bulletproof. So, off to the interwebs!&lt;/p&gt;
&lt;p&gt;Turns out, there&amp;#8217;s a big debate in and around the Rails community about the best layer at which to do this. Databases have support for this internally, by defining foreign keys in the database. So the smarts could easily go there.&lt;/p&gt;
&lt;p&gt;One problem: Rails isn&amp;#8217;t designed that way. None other than David Heinemeier Hansson, the original founder of the project, is firmly &lt;a href="http://david.heinemeierhansson.com/arc/2005_09.html"&gt;against it&lt;/a&gt;. (The permalinks are broken, so you&amp;#8217;ll have to scroll down to the entry entitled &amp;#8220;Choose a single layer of cleverness&amp;#8221;.)&lt;/p&gt;
&lt;p&gt;His reasoning is that these things are &amp;#8220;business logic&amp;#8221; - and your business logic should be in the application code, not the database. That&amp;#8217;s all well and good - except it&amp;#8217;s not that well documented. The current Rails Guide for 3.2 don&amp;#8217;t really talk about it, and the section on the :dependent option is &lt;a href="http://guides.rubyonrails.org/association_basics.html#belongs_to-dependent"&gt;incomplete&lt;/a&gt;, not even mentioning the option to prevent deleting the object if it&amp;#8217;s referenced from another object.&lt;/p&gt;
&lt;p&gt;I was finally able to find it in the API docs for &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many"&gt;has_many&lt;/a&gt;, but it wasn&amp;#8217;t easy. So now, in good open source fashion, I have a new to-do to write an update for the guide explaining all this&amp;#8230;&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/24832816364</link><guid>http://davidoncode.tumblr.com/post/24832816364</guid><pubDate>Sun, 10 Jun 2012 15:58:11 -0400</pubDate></item><item><title>Bayou Grand Prix: A Ruby on Rails project</title><description>&lt;p&gt;The &lt;a href="http://rubyonrails.org"&gt;Rails &lt;/a&gt;app I&amp;#8217;ve been poking at for a while is pretty frivolous. A fried of mine started a regular playing night for Formula De, a car racing game based on Formula 1 (and shame on them for racing in Bahrain). He wanted it done as a league, complete with teams, driver names, scoring, etc.  So in 2001, the &lt;a href="http://www.bayougrandprix.com"&gt;Bayou Grand Prix&lt;/a&gt; was born.&lt;/p&gt;
&lt;p&gt;I coded the first version of the web site - and as you can see, it&amp;#8217;s pretty ugly. My minimalist HTML design skills aside, it&amp;#8217;s coded in plain PHP on top of a SQL layer, and most of the logic is encoded in the SQL.  Given the scoring rules, and the need to support things like drivers racing for different teams in the same season, things got pretty ugly, with queries like this:&lt;/p&gt;
&lt;div class="gist"&gt;&lt;a href="https://gist.github.com/2650760"&gt;https://gist.github.com/2650760&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;And the administrative interface is pretty bad as well. I quite often have to go in and frob the database with raw SQL to fix things when I screw up the data entry - meaning that effectively I&amp;#8217;m the only one who can update the thing.&lt;/p&gt;
&lt;p&gt;So sometime in 2008 I started a project to write a new version in Rails. I&amp;#8217;ve been poking at it from time to time ever since, but it still hasn&amp;#8217;t reached feature parity with the current SQL site. Getting there, though.&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/22760411629</link><guid>http://davidoncode.tumblr.com/post/22760411629</guid><pubDate>Wed, 09 May 2012 23:06:00 -0400</pubDate><category>rails</category><category>bgp</category></item><item><title>Howdy</title><description>&lt;p&gt;I&amp;#8217;m a recovering product manager, trying to get back to my coding roots by fiddling with things like Rails and iOS. Every once in a while I&amp;#8217;ll run across something I feel I need to write about. Tumblr seems to be the flavor of the month, so I&amp;#8217;ll do that here.&lt;/p&gt;</description><link>http://davidoncode.tumblr.com/post/22758355673</link><guid>http://davidoncode.tumblr.com/post/22758355673</guid><pubDate>Wed, 09 May 2012 22:35:29 -0400</pubDate></item></channel></rss>
