<?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>ThePlant Japanese &#187; tech</title>
	<atom:link href="http:///ja/our/tech/feed/" rel="self" type="application/rss+xml" />
	<link>/ja</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 28 Jul 2010 08:38:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unpack Ruby Pack</title>
		<link>/ja/2009/10/unpack-ruby-pack/</link>
		<comments>/ja/2009/10/unpack-ruby-pack/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 02:24:00 +0000</pubDate>
		<dc:creator>jan</dc:creator>
				<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">/ja/?p=596</guid>
		<description><![CDATA[Today someone asked me an interesting question: how can I print a string in hexadecimal in ruby?]]></description>
			<content:encoded><![CDATA[<p>Today someone asked me an interesting question: how can I print a string in hexadecimal in ruby?</p>
<p>The solution that immediately jumped into my mind was something like &#8220;abc&#8221;.map {&#8230;}. The first problem here is #map is based on #each, and String#each will iterate on lines instead of characters. So &#8220;abc&#8221;.map {&#8230;} will have only 1 iteration, which give you access to &#8220;abc&#8221; as a whole inside the iteration.</p>
<p>It&#8217;s natural that I then went to String#each_byte for help. &#8220;abc&#8221;.each_byte {&#8230;} will iterate on characters. The problem with each_byte is its return value is the string itself, instead of the result after processing like map. We can&#8217;t get a one-line solution with each_byte:</p>
<pre class="code">res = []

"abc".each_byte {|i| res &lt;&lt; i}

res.map {|i| i.to_s(16)}</pre>
<p>3 lines for a simple problem! Ugly, right?</p>
<p>Then I remembered the swissarmy knife of ruby: String#unpack. String#unpack takes a string as directives and applies those directives on its caller. Its caller, a string, is used as a (binary) data stream in this process. For example:</p>
<pre class="code">"a".unpack('c') # =&gt; [97]</pre>
<p>&#8216;c&#8217; is a directive which will extract a character as an integer. You can apply many directives at the same time, like:</p>
<pre class="code">"abc".unpack('cH') # =&gt; [97, "6"]</pre>
<p>which will execute &#8216;c&#8217; on the first character, and &#8216;H&#8217; on the second. You can append a number or &#8216;*&#8217; to a directive so it will be executed many times:</p>
<pre class="code">"abc".unpack('c2') # =&gt; [97, 98]

"abc".unpack('c*') # =&gt; [97, 98, 99]</pre>
<p>And String#unpack provides us a directive to extract characters into a hex digit! Here&#8217;s the solution using unpack to my friend&#8217;s question:</p>
<pre class="code">"abc".unpack('H*')</pre>
<p>There are many directives you can use in unpack. Whenever you want to transform a string into another common format, like ascii, base64, please give <a href="http://www.ruby-doc.org/core/classes/String.html#M000760">String#unpack</a> a glance, it may resolve your problem instantly.</p>
]]></content:encoded>
			<wfw:commentRss>/ja/2009/10/unpack-ruby-pack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A helper method to create a lot of Hash objects</title>
		<link>/ja/2009/10/a-helper-method-to-create-a-lot-of-hash-objects/</link>
		<comments>/ja/2009/10/a-helper-method-to-create-a-lot-of-hash-objects/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 01:59:32 +0000</pubDate>
		<dc:creator>makoto</dc:creator>
				<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">/ja/?p=593</guid>
		<description><![CDATA[There are some situations in which you need to create a lot of Hash objects, such as creating dummy data for a test or benchmark.]]></description>
			<content:encoded><![CDATA[<p>There are some situations in which you need to create a lot of Hash objects, such as creating dummy data for a test or benchmark.</p>
<p>For example:</p>
<pre class="code">testdata = [
  { :name =&gt; "Haruhi", :gender =&gt; "F", :role =&gt; "Brigade Leader" },
  { :name =&gt; "Mikuru", :gender =&gt; "F", :role =&gt; "Time Traveler" },
  { :name =&gt; "Yuki",   :gender =&gt; "F", :role =&gt; "Humanoid Interface" },
  { :name =&gt; "Haruhi", :gender =&gt; "M", :role =&gt; "ESPer" },
  { :name =&gt; "Kyon",   :gender =&gt; "M", :role =&gt; "Normal" },
]</pre>
<p>But you may draw a sigh or be tired because you must type same keys many times.</p>
<p>In this case, the following helper method will help you.</p>
<pre class="code">class Hash
  def self.create_with(*keys)
    rows = yield()
    return rows.collect {|row|
      hash = self.new
      keys.zip(row) {|k, v| hash[k] = v }
      hash
    }
  end
end</pre>
<p>Using this helper method, you have to type keys only once.</p>
<pre class="code">testdata = Hash.create_with(:name, :gender, :role) {[
  [ "Haruhi", "F", "Brigade Leader"     ],
  [ "Mikuru", "F", "Time Traveler"      ],
  [ "Yuki",   "F", "Humanoid Interface" ],
  [ "Haruhi", "M", "ESPer"              ],
  [ "Kyon",   "M", "Normal"             ],
]}

p testdata[0]  #=&gt; {:gender=&gt;"F", :role=&gt;"Brigade Leader", :name=&gt;"Haruhi"}</pre>
<p>It is a good idea to use YAML format instead of array of arrays.</p>
<pre class="code">require 'yaml'
arrays = YAML.load &lt;&lt;END
- [ Haruhi, F, Brigade Leader     ]
- [ Mikuru, F, Time Traveler      ]
- [ Yuki,   F, Humanoid Interface ]
- [ Haruhi, M, ESPer              ]
- [ Kyon,   M, Normal             ]
END
testdata = Hash.create_with(:name, :gender, :role) { arrays }

p testdata[0]  #=&gt; {:gender=&gt;"F", :role=&gt;"Brigade Leader", :name=&gt;"Haruhi"}</pre>
<p>I hope this helps your Ruby life.</p>
]]></content:encoded>
			<wfw:commentRss>/ja/2009/10/a-helper-method-to-create-a-lot-of-hash-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>git-svn: Can&#8217;t locate SVN/Core.pm</title>
		<link>/ja/2009/07/git-svn-cant-locate-svncore-pm/</link>
		<comments>/ja/2009/07/git-svn-cant-locate-svncore-pm/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:13:38 +0000</pubDate>
		<dc:creator>sunfmin</dc:creator>
				<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">/ja/2009/07/git-svn-cant-locate-svncore-pm/</guid>
		<description><![CDATA[To import subversion repository to git, I get the following problem

Installing of the perl subversion binding solved this problem.

]]></description>
			<content:encoded><![CDATA[<p>To import subversion repository to git, I get the following problem</p>
<p><script src="http://gist.github.com/146704.js"></script></p>
<p>Installing of the perl subversion binding solved this problem.</p>
<p><script src="http://gist.github.com/146706.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>/ja/2009/07/git-svn-cant-locate-svncore-pm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install your own ssl certificate</title>
		<link>/ja/2009/07/install-your-own-ssl-certificate/</link>
		<comments>/ja/2009/07/install-your-own-ssl-certificate/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:12:52 +0000</pubDate>
		<dc:creator>sunfmin</dc:creator>
				<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">/ja/?p=423</guid>
		<description><![CDATA[First you must create your ssl certificate

You setup your certificate in apache config file

]]></description>
			<content:encoded><![CDATA[<p>First you must create your ssl certificate</p>
<p><script src="http://gist.github.com/146701.js"></script></p>
<p>You setup your certificate in apache config file</p>
<p><script src="http://gist.github.com/146702.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>/ja/2009/07/install-your-own-ssl-certificate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tell postfix not to send email to local</title>
		<link>/ja/2009/07/tell-postfix-not-to-send-email-to-local/</link>
		<comments>/ja/2009/07/tell-postfix-not-to-send-email-to-local/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:12:07 +0000</pubDate>
		<dc:creator>sunfmin</dc:creator>
				<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">/ja/2009/07/tell-postfix-not-to-send-email-to-local/</guid>
		<description><![CDATA[We want to config the server can only send emails, can NOT receive emails from local or remote. Because our domain is pointed to this server, But our email server is on Google Enterprise. So before If we want to send email from the server, It first looks up locally, and send to local directly, [...]]]></description>
			<content:encoded><![CDATA[<p>We want to config the server can only send emails, can NOT receive emails from local or remote. Because our domain is pointed to this server, But our email server is on Google Enterprise. So before If we want to send email from the server, It first looks up locally, and send to local directly, So emails never get reach to Google.</p>
<p>I tried: http://www.postfix.org/faq.html#null_client But I seems not work.</p>
<p>After check out the postfix log:</p>
<p><script src="http://gist.github.com/146697.js"></script></p>
<p>Even you set</p>
<p>local_transport = error:local delivery is disabled<br />
But when you have aliasmaps, aliasdatabase, virtualaliasmaps. Postfix will still try to find the alias name in local. So it fails when it can not find it.</p>
<p>main.cf</p>
<p><script src="http://gist.github.com/146699.js"></script></p>
<p>master.cf</p>
<p><script src="http://gist.github.com/146700.js"></script></p>
<p>And Another thing, make sure your hostname is NOT your domain. because It will check hostname first and mx record? I guess.</p>
]]></content:encoded>
			<wfw:commentRss>/ja/2009/07/tell-postfix-not-to-send-email-to-local/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
