<?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>Khelll&#039;s Blog &#187; Encoding</title>
	<atom:link href="http://khelll.com/blog/tag/encoding/feed/" rel="self" type="application/rss+xml" />
	<link>http://khelll.com/blog</link>
	<description>Cool Web Development...</description>
	<lastBuildDate>Thu, 13 Oct 2011 03:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ruby 1.9 Encoding Fun</title>
		<link>http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/</link>
		<comments>http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 13:52:23 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Encoding]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=517</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/' addthis:title='Ruby 1.9 Encoding Fun '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Ruby 1.9 has a great support for encoding, this post covers that for a big deal, however with this support out of the box, things like this becomes easier, just note the source code is not limited to few types &#8230; <a href="http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/' addthis:title='Ruby 1.9 Encoding Fun' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/' addthis:title='Ruby 1.9 Encoding Fun '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>Ruby 1.9 has a great support for encoding, <a href="http://blog.nuclearsquid.com/writings/ruby-1-9-encodings">this</a> post covers that for a big deal, however with this support out of the box, things like <a href="http://www.oreillynet.com/ruby/blog/2007/10/fun_with_unicode_1.html">this</a> becomes easier, just note the source code is not limited to few types of encoding just like Ruby 1.8.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># coding: UTF-8</span>
&nbsp;
π = <span style="color:#CC00FF; font-weight:bold;">Math</span>::PI
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> √<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span> 
  <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> ∑<span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span>
  r.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>sum,val<span style="color:#006600; font-weight:bold;">|</span> sum <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span>block_given? ? <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>val<span style="color:#006600; font-weight:bold;">&#41;</span> : val<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span> 
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> ∏<span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span>
  r.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>mul,val<span style="color:#006600; font-weight:bold;">|</span> mul <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span>block_given? ? <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>val<span style="color:#006600; font-weight:bold;">&#41;</span> : val<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span> 
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> ¬<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
  !x
<span style="color:#9966CC; font-weight:bold;">end</span>	
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> π <span style="color:#008000; font-style:italic;">#=&gt; 3.14159265358979</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> √<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 3.0</span>
&nbsp;
range = <span style="color:#006666;">1</span>..<span style="color:#006666;">6</span> 
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> ∑<span style="color:#006600; font-weight:bold;">&#40;</span>range<span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;">#=&gt; 21</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> ∑<span style="color:#006600; font-weight:bold;">&#40;</span>range<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#008000; font-style:italic;">#=&gt; 91</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> ∏<span style="color:#006600; font-weight:bold;">&#40;</span>range<span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;">#=&gt; 720</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> ∏<span style="color:#006600; font-weight:bold;">&#40;</span>range<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#008000; font-style:italic;">#=&gt; 518400</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> ¬<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; false</span></pre></div></div>

<p><strong>Please note that previous example could work also with Ruby 1.8 using the flag<code> -Ku</code>, I just wanted to explain the idea of using different types of encoding in Ruby 1.9.</strong></p>
<p>So all you need to do is to set whatever encoding you want for your source code at the very beginning of the file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># coding: UTF-8</span></pre></div></div>

<p>Many other things can be done, just give yourself a moment and think what you can do!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/' addthis:title='Ruby 1.9 Encoding Fun' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/ruby-1-9-encoding-fun/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

