<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Basic tech stuff</title>
	<atom:link href="http://basic70tech.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://basic70tech.wordpress.com</link>
	<description>Programming and Linux administration</description>
	<lastBuildDate>Mon, 23 Jan 2012 07:16:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='basic70tech.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Basic tech stuff</title>
		<link>http://basic70tech.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://basic70tech.wordpress.com/osd.xml" title="Basic tech stuff" />
	<atom:link rel='hub' href='http://basic70tech.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Unix: select vs poll</title>
		<link>http://basic70tech.wordpress.com/2012/01/23/unix-select-vs-poll/</link>
		<comments>http://basic70tech.wordpress.com/2012/01/23/unix-select-vs-poll/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 05:10:21 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/?p=27</guid>
		<description><![CDATA[Asch, det här får bli på svenska för ovanlighetens skull. Så länge jag kan minnas så har jag använt funktionen select() för att vänta på data på en socket. Koden för att använda den ser ut ungefär så här: fd_set fdset; struct timeval tv; FD_ZERO(&#38;fdset); FD_SET(fd, &#38;fdset); timeout.tv_sec = 60; timeout.tv_usec = 0; switch(select(fd + [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=27&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Asch, det här får bli på svenska för ovanlighetens skull.</p>
<p>Så länge jag kan minnas så har jag använt funktionen select() för att vänta på data på en socket. Koden för att använda den ser ut ungefär så här:</p>
<blockquote><p>fd_set fdset;<br />
struct timeval tv;</p>
<p>FD_ZERO(&amp;fdset);<br />
FD_SET(fd, &amp;fdset);</p>
<p>timeout.tv_sec = 60;<br />
timeout.tv_usec = 0;</p>
<p>switch(select(fd + 1, &amp;fdset, NULL, NULL, &amp;timeout)) {<br />
case -1: ;/* something went wrong, check errno */<br />
case 1: ; /* read data */<br />
default: ; /* timeout */<br />
}</p></blockquote>
<p>Hos en kund fick vi plötsligt bara timeouter. Trots ett fint select-anrop med rätt parametrar, så sa den aldrig till när det fanns data att läsa, eller när det var ok att skicka tillbaka ett svar.</p>
<p>Problemet visade sig vara den där fd_set-saken. I man-sidan till select() står nämligen följande:</p>
<blockquote><p>The behavior of these macros is undefined if the fd argument is less than 0 or greater<br />
than or equal to FD_SETSIZE, &#8230;</p></blockquote>
<p>Så vad är värdet på FD_SETSIZE? Jo, både i Linux och Solaris är det 1024. I de logfiler vi undersökte så var fd mellan 1025 och 1028, eftersom programmet hade ganska många aktiva uppkopplingar igång. Hoppsan.</p>
<p>Alltså var select() oanvändbar för vår del, så vi fick byta till funktionen poll(), som funkar lite annorlunda. Istället för att ta en bitmap där man har markerat vilka filer man vill vänta på, så skickar man in en array med filnummer. Filnumret blir irrelevant och kan vara hur stort som helst, och det tar inte längre tid oavsett hur stort det än är. Stackars select() måste ju gå igenom hela bitmappen för att hitta vilka bittar man har satt, vilket tar lite tid.</p>
<p>Det här är för övrigt precis det byte av perspektiv för att få bättre Ordo-värde som jag skrev om i höstens nanowrimo, för er typ 2-3 personer som har läst den. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Koden blir lite annorlunda, även om principen är densamma.</p>
<blockquote><p>struct pollfd pfd;<br />
pfd.fd = fd;<br />
pfd.events = POLLIN;<br />
rc = poll(&amp;pfd, 1, 60 * 1000);<br />
if ((rc &lt; 0) || (pfd.revents &amp; (POLLERR | POLLNVAL | POLLHUP))) { } /* fail */<br />
else if (pfd.revents &amp; POLLIN) { } /* read data */<br />
else { } /* timeout */</p></blockquote>
<p>Refaktorerat till en &#8220;finns det data att läsa på socket &#8216;fd&#8217; inom x sekunder?&#8221;-funktion blev den nya koden riktigt smidig. Förut fanns en handfull olika anrop till select() som alla gjorde lite, lite olika när det gällde felhanteringen. Att flytta in det hela i en separat funktion gör att det är lätt att justera parametrarna till poll() om det skulle behövas, att det är lätt att logga vartenda anrop, och att samma typ av fel alltid rapporteras uppåt på exakt samma sätt.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=27&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2012/01/23/unix-select-vs-poll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>JNI</title>
		<link>http://basic70tech.wordpress.com/2008/01/30/jni/</link>
		<comments>http://basic70tech.wordpress.com/2008/01/30/jni/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 10:52:50 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/?p=25</guid>
		<description><![CDATA[Jay me! By using the example from the JNI documentation, fixing the errors, and then figuring out the not entirely obvious command lines for compiling, linking and running it, I now have a C program that can talk to Java. On my Fedora Core 3 box I had to build and run it like this: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=25&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Jay me!</p>
<p>By using the <a href="http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniref.html#invoke" target="_blank">example from the JNI documentation</a>, fixing the errors,  and then figuring out the not entirely obvious command lines for compiling, linking and running it, I now have a C program that can talk to Java.</p>
<p>On my Fedora Core 3 box I had to build and run it like this:</p>
<p><code>gcc -o runjava runjava.c -L/usr/java/jdk1.5.0_11/jre/lib/i386/client -ljvm<br />
LD_LIBRARY_PATH=/usr/java/jdk1.5.0_11/jre/lib/i386/client ./runjava</code></p>
<p>I bet there is some nice shortcut for that, like `javaconfig -cflags` or something, but I haven&#8217;t found that one yet.</p>
<p>Using JNI doesn&#8217;t seem entirely difficult, especially since we&#8217;re only going to use very few arguments, and the same method signature for all calls. I hope our customers will like that they don&#8217;t have to write plugins in C anymore, but can use nicer languages.</p>
<p>Andra bloggar om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>, <a href="http://bloggar.se/om/java" rel="tag">java</a>, <a href="http://bloggar.se/om/jni" rel="tag">jni</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/25/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/25/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=25&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2008/01/30/jni/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>Never fetch data from views</title>
		<link>http://basic70tech.wordpress.com/2007/08/22/never-fetch-data-from-views/</link>
		<comments>http://basic70tech.wordpress.com/2007/08/22/never-fetch-data-from-views/#comments</comments>
		<pubDate>Wed, 22 Aug 2007 07:52:18 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/08/22/never-fetch-data-from-views/</guid>
		<description><![CDATA[Jeez, when am I going to learn? I know very well that all data that should be displayed in view must be fetched by the controller. View must never, ever, under any circumstances, fetch data by themselves. Still, I manage to break that rule all too often. &#8220;It&#8217;s just going to fetch THIS little piece [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=24&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Jeez, when am I going to learn?</p>
<p>I know very well that all data that should be displayed in view must be fetched by the controller.</p>
<blockquote><p>View must never, ever, under any circumstances, fetch data by themselves.</p></blockquote>
<p>Still, I manage to break that rule all too often. &#8220;It&#8217;s just going to fetch THIS little piece of data&#8221;. This not only includes SQL statements, but even all kinds of accessors that do more than fetch complete objects from a hashtable or similar.</p>
<p>A while ago I wrote a little find_safely() helper method for my ApplicationController, that handles &#8220;Record not found&#8221; errors. All find() accesses went through that method, which meant that when the database had to be separated into different systems, which meant that most tables had to have a system_id column, only that single method had to be changed. Yes, the value had to be set in a few places, but it never fetched data for the wrong system.</p>
<p>Unfortunately some views fetched data themselves, which meant that I had to have the system_id check in both ApplicationController and ApplicationHelper. This quickly got out of hand, so it didn&#8217;t take long until I moved all those accesses back to the proper controller. When this was done, it was also possible to test that the views were showing the correct data. The normal Rails test harness doesn&#8217;t have access to the data that the views fetch, which means it can&#8217;t be tested. After the refactoring, it could.</p>
<p>This brings back another of my favourite arguments:</p>
<blockquote><p>You should write tests, not only to verify that the application does what it is supposed to (you&#8217;ll find that out soon enough anyway), but because it forces you to build the application in a better way.</p></blockquote>
<p>Anyway, the &#8220;data from views&#8221; problem came back to bite me once again today. The application stores a list of the most recently accessed business objects of each type. This was tested by directly checking the contents of the array where they were stored.</p>
<p>It worked fine, until the objects grew too large, overflowing the &#8220;data&#8221; column in the sessions table, which in turned caused a &#8220;marshal data too short&#8221; error too appear in the Mongrel log file. Most pages recommended using a larger column type such as mediumtext, but this is of course a bad idea. Having the poor application read and parse several hundred KB or more on each request, only to serialize it back and update the database afterwards kills performance. A couple of KB is ok, but the 64KB limit of the &#8220;text&#8221; type is there for a reason.</p>
<p>The proper solution is of course to only store the object id&#8217;s, and instanciate the objects only when needed. The application worked again, but now the hard coded test cases started to break. To get them to work, the controllers had to fetch this data, which in turn meant that the views didn&#8217;t have to.</p>
<p>Instead of an ugly Module that was imported by both ApplicationController and ApplicationHelper, the &#8220;latest objects&#8221; code could be merged back into ApplicationController where it belong. The code became simpler, the response time decreased, the database errors disappeared, and a larger part of the code got proper unit tests. Sure, just using &#8220;mediumtext&#8221; would have been faster in terms of work hours, but the code quality would have suffered.</p>
<p>Andra bloggar <a href="http://intressant.se/intressant" rel="tag">intressant</a> om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>, <a href="http://bloggar.se/om/testning" rel="tag">testning</a>, <a href="http://bloggar.se/om/rails" rel="tag">rails</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=24&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/08/22/never-fetch-data-from-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>Basic&#8217;s programming laws</title>
		<link>http://basic70tech.wordpress.com/2007/08/01/basics-programming-laws/</link>
		<comments>http://basic70tech.wordpress.com/2007/08/01/basics-programming-laws/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 09:15:03 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/08/01/basics-programming-laws/</guid>
		<description><![CDATA[I&#8217;ve come to realize that the following laws apply when debugging a program. A program that runs on several platforms will fail on the one with the worst tools for debugging. Having gdb installed on one machine while being absent on another, makes the bug appear on the second machine. Having Purify or Valgrind installed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=23&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come to realize that the following laws apply when debugging a program.</p>
<ol>
<li>A program that runs on several platforms will fail on the one with the worst tools for debugging.
<ol>
<li>Having gdb installed on one machine while being absent on another, makes the bug appear on the second machine.</li>
<li>Having Purify or Valgrind installed on a subset of the platforms, guarantees that the bugs will occur only on the ones without these tools.</li>
</ol>
</li>
<li>With identical toolsets, the bugs will occur on the machine that is hardest to access.
<ol>
<li>With ssh access to two machines, the bug will occur on the one to which you can&#8217;t use keybased login.</li>
<li>If one machine requires Cisco VPN login, which never seems to work correctly, that&#8217;s the machine which will fail.</li>
</ol>
</li>
<li>If the program is multithreaded, the bug will only appear when running outside of the debugger, since these tools always affect the relative timing of the threads.</li>
</ol>
<p>On the other hand, the reason I&#8217;m a programmer is not because it&#8217;s easy, but because it&#8217;s hard.</p>
<p>Andra bloggar om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=23&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/08/01/basics-programming-laws/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails fix for nested forms</title>
		<link>http://basic70tech.wordpress.com/2007/05/26/rails-fix-for-nested-forms/</link>
		<comments>http://basic70tech.wordpress.com/2007/05/26/rails-fix-for-nested-forms/#comments</comments>
		<pubDate>Sat, 26 May 2007 21:19:34 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/05/26/rails-fix-for-nested-forms/</guid>
		<description><![CDATA[In Rails there is a nice helper function button_to. It uses Javascript in the onclick event to create a new form which is immediately submitted. Perfect when a button is all you really need. Or so I thought, until I installed the HTML Validator extension to Firefox. It has a super strict HTML parser that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=22&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In Rails there is a nice helper function <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M000486" target="_blank">button_to</a>. It uses Javascript in the <code>onclick</code> event to create a new form which is immediately submitted. Perfect when a button is all you really need.</p>
<p>Or so I thought, until I installed the <a href="http://users.skynet.be/mgueury/mozilla/" target="_blank">HTML Validator</a> extension to Firefox. It has a super strict HTML parser that tells you when something is wrong with your code. This way there is a much greater chance that the pages will look the way you want in as many browsers as possible.</p>
<p>So what&#8217;s the problem? Well, I had a <code>form</code> surrounding a <code>table</code>. The form was really only for the last row in the table, but since a form must either be outside of the <code>table</code> tag or within a <code>td</code>, the only option was to let the form surround the table. On all rows but the one with the <em>real</em> form fields, I wanted that new button. When the new form was created, the HTML validator became a bit upset about the fact that a new form was created within an existing form. This isn&#8217;t allowed. (There are sure a lot of things that aren&#8217;t allowed in HTML.)</p>
<p>The solution was to patch actionpack/lib/action_view/helpers/url_helper.rb around line 367, changing</p>
<pre>    "this.body.appendChild(f); f.method = 'POST'; f.action = this.href;"</pre>
<p>into</p>
<pre>    "document.body.appendChild(f); f.method = 'POST'; f.action = this.href;"</pre>
<p>This makes the new form live as a child to the top level body tag instead. Now the validator was happy again.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=22&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/05/26/rails-fix-for-nested-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>Reasons for automatic tests</title>
		<link>http://basic70tech.wordpress.com/2007/05/22/reasons-for-automatic-tests/</link>
		<comments>http://basic70tech.wordpress.com/2007/05/22/reasons-for-automatic-tests/#comments</comments>
		<pubDate>Tue, 22 May 2007 08:31:41 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/05/22/reasons-for-automatic-tests/</guid>
		<description><![CDATA[As everybody should know by now, there are plenty of reasons for writing automatic tests. One of them, of course, being the fact that you get to see whether your application actually works, at least for some cases. That&#8217;s just a small part of the story, though. Reason number two is design. To be able [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=21&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As everybody should know by now, there are plenty of reasons for writing automatic tests. One of them, of course, being the fact that you get to see whether your application actually <em>works</em>, at least for some cases. That&#8217;s just a small part of the story, though.</p>
<p>Reason number two is <em>design</em>. To be able to write both small unit tests, large system tests and everything in between, the application simply must be well designed. Otherwise it will be impossible to test one thing at a time, and mock out the parts that should be faked. Knowing where to draw the lines between the modules in a system can be difficult, but by simply trying to write nice tests for them, it becomes much easier.</p>
<p>The third reason is <em>refactoring</em>, which I personally got bitten by this weekend when making a change to my <a href="http://rssping.ricercar.se" target="_blank">RSS/Ping</a> service, written in Ruby on Rails. In the first versions the &#8220;ping&#8221; logic was a separate program, but the users wanted it merged with the web application. So, I very carefully moved one function at a time into the classes where they belonged, and made a little button in the web interface. Despite being exactly the same code, with no uninitialized variables or anything like that, it simply refused to work.</p>
<p>Today I found the problem. The standalone program used a couple of <code>require</code> statements to import modules for RSS and Atom parsing and whatnot, things that weren&#8217;t used in the web application. This made the function fail, even though everything was technically fully correct. The standalone program worked, so tests wasn&#8217;t really necessary, I thought.</p>
<p>Lesson learned? Automatic tests are good, and should aim for full code coverage. This makes the programming part so much easier.</p>
<p>Andra bloggar <a href="http://intressant.se/intressant" rel="tag">intressant</a> om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>, <a href="http://bloggar.se/om/ruby+on+rails" rel="tag">ruby on rails</a>, <a href="http://bloggar.se/om/testning" rel="tag">testning</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=21&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/05/22/reasons-for-automatic-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails: belongs_to :polymorphic and inheritance</title>
		<link>http://basic70tech.wordpress.com/2007/05/10/rails-belongs_to-polymorphic-and-inheritance/</link>
		<comments>http://basic70tech.wordpress.com/2007/05/10/rails-belongs_to-polymorphic-and-inheritance/#comments</comments>
		<pubDate>Thu, 10 May 2007 07:44:11 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/05/10/rails-belongs_to-polymorphic-and-inheritance/</guid>
		<description><![CDATA[The flag :polymorphic option for belongs_to associations is extremely useful, especially when implementing an authorization system. Let&#8217;s say that I have a Permission class that points to either a House or a Car: class Permission &#60; ActiveRecord::Base; belongs_to :authobject, :polymorphic =&#62; true; end class House &#60; ActiveRecord::Base; has_many :permissions, :as =&#62; 'authobject'; end class Car [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=20&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The flag <code>:polymorphic</code> option for <code>belongs_to</code> associations is extremely useful, especially when implementing an authorization system. Let&#8217;s say that I have a <code>Permission</code> class that points to either a <code>House</code> or a <code>Car</code>:<br />
<code><br />
class Permission &lt; ActiveRecord::Base; belongs_to :authobject, :polymorphic =&gt; true; end<br />
class House &lt; ActiveRecord::Base; has_many :permissions, :as =&gt; 'authobject'; end<br />
class Car &lt; ActiveRecord::Base; has_many :permissions, :as =&gt; 'authobject'; end<br />
</code></p>
<p>This works just fine, making it possible both to find the object that a <code>Permission</code> is for, and the relevant <code>Permissions</code> for a certain object. In the column <code>authobject_type</code> you get the strings &#8220;House&#8221; and &#8220;Car&#8221;, respectively.</p>
<p>Now the problem: We want to replace the <code>Car</code> class with an abstract <code>Vehicle</code> class, with subclasses <code>Car</code> and <code>Boat:</code></p>
<p><code>class Vehicle &lt; ActiveRecord::Base; has_many :permissions, :as =&gt; 'authobject'; end<br />
class Car &lt; Vehicle; end<br />
class Boat &lt; Vehicle; end<br />
</code></p>
<p>In Rails version 1.2.3 this puts the string &#8220;Vehicle&#8221; in the <code>Permissions.authobject_type</code> column, which causes lots of stuff to fail. In my case, a bunch of test cases that simply created a <code>Permission</code> and made sure it could be found again. Suddenly it couldn&#8217;t.</p>
<p>The problem was this bug: <a href="http://dev.rubyonrails.org/ticket/6485">http://dev.rubyonrails.org/ticket/6485</a>, with a patch that makes sure that the real class name in the <code>authobject_type</code> field. The <a href="http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000644" target="_blank">Rails documentation</a> says you should store the base class in the <code>authobject_type</code> field, but that simply isn&#8217;t right, since it makes it impossible to load the right subclass. Since we still want to store permissions to Houses, it&#8217;s important that we store the exact class name.</p>
<p>Edit 2007-05-16: The problem is still not completely solved, since <code>:dependent =&gt; :destroy</code> still uses the base class name instead of the correct one. Since following the relationship works, you have to loop and destroy the objects manually.</p>
<p>Andra bloggar <a href="http://intressant.se/intressant" rel="tag">intressant</a> om: <a href="http://bloggar.se/om/rails" rel="tag">rails</a>, <a href="http://bloggar.se/om/programming" rel="tag">programming</a>, <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/20/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/20/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=20&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/05/10/rails-belongs_to-polymorphic-and-inheritance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>This is NOT the HD-DVD key</title>
		<link>http://basic70tech.wordpress.com/2007/05/02/this-is-not-the-hd-dvd-key/</link>
		<comments>http://basic70tech.wordpress.com/2007/05/02/this-is-not-the-hd-dvd-key/#comments</comments>
		<pubDate>Wed, 02 May 2007 16:40:25 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[encryption]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/05/02/this-is-not-the-hd-dvd-key/</guid>
		<description><![CDATA[Just as normal DVD&#8217;s are encrypted, so are HD-DVD&#8217;s. In the latter case both a global key and a bunch of player specific keys are used. First the latter ones were found for a particular player, and a while ago the global key was found. Using this, and some software, any HD-DVD can be played [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=19&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just as normal DVD&#8217;s are encrypted, so are HD-DVD&#8217;s. In the latter case both a global key and a bunch of player specific keys are used. First the latter ones were found for a particular player, and a while ago the global key was found. Using this, and some software, any HD-DVD can be played and copied to a hard drive for later viewing.</p>
<p>Today Slashdot has an <a href="http://yro.slashdot.org/article.pl?sid=07/05/01/1935250" target="_blank">article about the censoring of this number</a>. Writing it seems illegal, but I ought to be able to write  that it is NOT f6 06 ee fd 62 8b 1c a4 27 be a9 3a 9c a9 77 3f. (Thanks to <a href="http://yro.slashdot.org/comments.pl?sid=233031&amp;cid=18948387" target="_blank">AJVM</a> for the tip.)</p>
<p>Damn, the movie industry is stupid.</p>
<p>Andra bloggar <a href="http://intressant.se/intressant" rel="tag">intressant</a> om: <a href="http://bloggar.se/om/encryption" rel="tag">encryption</a>, <a href="http://bloggar.se/om/hddvd" rel="tag">hddvd</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=19&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/05/02/this-is-not-the-hd-dvd-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>The Ruby module Enumeration is fun</title>
		<link>http://basic70tech.wordpress.com/2007/04/21/18/</link>
		<comments>http://basic70tech.wordpress.com/2007/04/21/18/#comments</comments>
		<pubDate>Sat, 21 Apr 2007 19:53:55 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/04/21/18/</guid>
		<description><![CDATA[In a Ruby application I had a bunch of data in a hash table that I wanted to print in a special format. There were a couple of requirements: Some of the options should be ignored. The format should be &#8220;key:value&#8221;. The list should be sorted. The entries should be separated by a space. Fortunately [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=18&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a Ruby application I had a bunch of data in a hash table that I wanted to print in a special format. There were a couple of requirements:</p>
<ol>
<li>Some of the options should be ignored.</li>
<li>The format should be &#8220;key:value&#8221;.</li>
<li>The list should be sorted.</li>
<li>The entries should be separated by a space.</li>
</ol>
<p>Fortunately this sort of thing is dead easy in Ruby, because of all of the cool functions in the Enumeration module. By stacking them after each other, I got this:</p>
<pre>KILL_LIST = [ 1, 42, 312 ]
def hash_for_print(hash_data)
  hash_data.
    reject {|key, value| KILL_LIST.include?(key)}.
    collect {|key,value| "%03d:%s" % [key, value]}.
    sort.
    join(" ")
end</pre>
<p>No, it&#8217;s not something I&#8217;d use in the innermost loop of a realtime system. That&#8217;s irrelevant. The code is dead easy to understand and modify, and didn&#8217;t take much time to write. Besides, it scales linearly, which is quite important.</p>
<p>Andra bloggar om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>, <a href="http://bloggar.se/om/ruby" rel="tag">ruby</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=18&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/04/21/18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
		<item>
		<title>32 bit IP address to dotted notation in Ruby</title>
		<link>http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/</link>
		<comments>http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 07:37:56 +0000</pubDate>
		<dc:creator>Daniel Brahneborg</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/</guid>
		<description><![CDATA[In one of our applications we store an IP address as a 32 bit integer. To show the value of this field it must be converted to normal dotted notation, and then back again to an integer to get stored in the database. Going from dotted notation is easy: require 'ipaddr' IPAddr.new('1.2.3.4').to_i Or, the &#8220;manual&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=17&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In one of our applications we store an IP address as a 32 bit integer. To show the value of this field it must be converted to normal dotted notation, and then back again to an integer to get stored in the database.</p>
<p>Going from dotted notation is easy:</p>
<p><code>require 'ipaddr'<br />
IPAddr.new('1.2.3.4').to_i</code></p>
<p>Or, the &#8220;manual&#8221; version:</p>
<p><code>'1.2.3.4'.split('.').inject(0) {|total,value| (total &lt;&lt; 8 ) + value.to_i}</code></p>
<p>I couldn&#8217;t find any examples of going from an integer to dotted notation, so I ended up with this:</p>
<p><code>address = 0x01020304<br />
[24, 16, 8, 0].collect {|b| (address &gt;&gt; b) &amp; 255}.join('.')</code></p>
<p>Andra bloggar <a href="http://intressant.se/intressant" rel="tag">intressant</a> om: <a href="http://bloggar.se/om/programmering" rel="tag">programmering</a>, <a href="http://bloggar.se/om/ruby" rel="tag">ruby</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/basic70tech.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/basic70tech.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/basic70tech.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/basic70tech.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/basic70tech.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=basic70tech.wordpress.com&amp;blog=796128&amp;post=17&amp;subd=basic70tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">basic</media:title>
		</media:content>
	</item>
	</channel>
</rss>
