<?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>dom111.co.uk &#187; Ruby</title>
	<atom:link href="http://www.dom111.co.uk/blog/category/coding/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dom111.co.uk/blog</link>
	<description>Move along. Nothing to see here.</description>
	<lastBuildDate>Wed, 26 Oct 2011 16:37:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>String Variable Concatenation</title>
		<link>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149</link>
		<comments>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149#comments</comments>
		<pubDate>Fri, 14 Aug 2009 13:27:09 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[string concatenation]]></category>
		<category><![CDATA[variable concatenation]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=149</guid>
		<description><![CDATA[I stumbled across something odd today in PHP: $r = ''; $r .= $r .= $r .= 'a'; Now, personally, I&#8217;d have expected a syntax error from the above code, but the result was even more confusing at first&#8230; print $r; // 'aaaa' Not sure if this was the expected output or not I tested [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled across something odd today in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, personally, I&#8217;d have expected a syntax error from the above code, but the result was even more confusing at first&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 'aaaa'</span></pre></div></div>

<p>Not sure if this was the expected output or not I tested similar code in other languages:</p>
<p>Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">r = <span style="color:#996600;">''</span>
r <span style="color:#006600; font-weight:bold;">+</span>= r <span style="color:#006600; font-weight:bold;">+</span>= r <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">'a'</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> r <span style="color:#008000; font-style:italic;"># 'a'</span></pre></div></div>

<p>Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">r = <span style="color: #483d8b;">''</span>
r += r += r += <span style="color: #483d8b;">'a'</span>
<span style="color: #808080; font-style: italic;">#   File &quot;&quot;, line 1</span>
<span style="color: #808080; font-style: italic;">#     r += r += r += 'a'</span>
<span style="color: #808080; font-style: italic;">#             ^</span>
<span style="color: #808080; font-style: italic;"># SyntaxError: invalid syntax</span></pre></div></div>

<p>Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
r <span style="color: #339933;">+=</span> r <span style="color: #339933;">+=</span> r <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'a'</span></pre></div></div>

<p>Perl:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$r</span><span style="color: #339933;">;</span> <span style="color: #339933;">//</span> <span style="color: #ff0000;">'aaaa'</span></pre></div></div>

<p>That explains it!</p>
<p>So the reason the string is &#8216;aaaa&#8217; seems to be that the code is evaluated from right to left:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// How it works:</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// $r += 'a'; // 'a'</span>
<span style="color: #666666; font-style: italic;">// $r += $r += 'a'; // 'a' + 'a';</span>
<span style="color: #666666; font-style: italic;">// $r += $r += $r += 'a'; // 'aa' + ('a' + 'a')</span></pre></div></div>

<p>I don&#8217;t think it&#8217;s a bug, well, at least I assume not, but is there a name for this?</p>
<p><strong>Update</strong>: I <a href="http://www.reddit.com/r/PHP/comments/9amid/string_concatenation/">asked some clever people for help understanding it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>chunk &#8211; Read a large (XML) file a chunk at a time</title>
		<link>http://www.dom111.co.uk/blog/coding/chunk-read-a-large-xml-file-a-chunk-at-a-time/99</link>
		<comments>http://www.dom111.co.uk/blog/coding/chunk-read-a-large-xml-file-a-chunk-at-a-time/99#comments</comments>
		<pubDate>Fri, 10 Jul 2009 12:35:28 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[parse large xml file]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=99</guid>
		<description><![CDATA[I&#8217;ve recently had to parse some pretty large XML documents, and needed a method to read one element at a time. Here&#8217;s a fairly simple solution in PHP and Ruby form (hopefully a Python one coming soon&#8230;). If you have the following file (complex-test.xml): &#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62; &#60;Complex&#62; &#60;Object&#62; &#60;Title&#62;Title 1&#60;/Title&#62; &#60;Name&#62;It's name goes here&#60;/Name&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently had to parse some pretty large XML documents, and needed a method to read one element at a time.</p>
<p>Here&#8217;s a fairly simple solution in <a href="/files/chunk/chunk-php-0.1.tar.gz">PHP</a> and <a href="/files/chunk/chunk-rb-0.1.tar.gz">Ruby</a> form (hopefully a Python one coming soon&#8230;).</p>
<p><span id="more-99"></span>If you have the following file (complex-test.xml):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Complex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Title 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>It's name goes here<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ObjectData<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Info1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Info1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Info2<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Info2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Info3<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Info3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Info4<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Info4<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ObjectData<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Date<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Date<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Object<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;AnotherObject<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/AnotherObject<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Data<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Object<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Object<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Complex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And wanted to return the <code>&lt;Object/&gt;</code>s</p>
<p>PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.chunk.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Chunk<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'complex-test.xml'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'element'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Object'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// do some parsing, insert to DB whatever</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'chunk.rb'</span>
&nbsp;
file = Chunk.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'complex-test.xml'</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'element'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Object'</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> xml = file.<span style="color:#9900CC;">read</span>
  doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>xml<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">//</span> <span style="color:#9966CC; font-weight:bold;">do</span> some parsing, blah...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It (probably) doesn&#8217;t work with nested XML elements, but the use I had for it didn&#8217;t require that it did.</p>
<p>You can get the <a href="/files/chunk/chunk-php-0.1.tar.gz">PHP version here</a>, and <a href="/files/chunk/chunk-rb-0.1.tar.gz">the Ruby one here</a>.</p>
<p><strong>Update</strong>: The <a href="http://www.phpclasses.org/phpchunk">class was accepted onto PHP Classes</a> as a notable package! 04/08/2009.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/chunk-read-a-large-xml-file-a-chunk-at-a-time/99/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails: image_submit_tag and :confirm</title>
		<link>http://www.dom111.co.uk/blog/coding/ruby-on-rails-image_submit_tag-and-confirm/26</link>
		<comments>http://www.dom111.co.uk/blog/coding/ruby-on-rails-image_submit_tag-and-confirm/26#comments</comments>
		<pubDate>Thu, 14 Aug 2008 17:10:00 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=26</guid>
		<description><![CDATA[I was told about an issue today with the image_submit_tag not supporting the :confirm method on the element: 1 &#60;%= image_submit_tag&#40;'buttons/submit.gif', &#123; :confirm =&#62; 'Are you sure?' &#125;&#41; %&#62; so I thought I&#8217;d share a quick fix which would be the actual javascript returned by the function: 1 2 &#60;%= image_submit_tag&#40;'buttons/submit.gif', &#123; :onclick =&#62; 'return [...]]]></description>
			<content:encoded><![CDATA[<p>I was told about an issue today with the image_submit_tag not supporting the :confirm method on the element:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= image_submit_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'buttons/submit.gif'</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Are you sure?'</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></pre></td></tr></table></div>

<p>so I thought I&#8217;d share a quick fix which would be the actual javascript returned by the function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= image_submit_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'buttons/submit.gif'</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:onclick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'return
confirm(<span style="color:#000099;">\'</span>Are you sure?<span style="color:#000099;">\'</span>);'</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></pre></td></tr></table></div>

<p>Hope this can save some people time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/ruby-on-rails-image_submit_tag-and-confirm/26/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

