<?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>aproxacs</title>
	<atom:link href="http://aproxacs.blog.qrobo.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://aproxacs.blog.qrobo.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Jul 2009 23:07:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating MSN messenger bot with ruby.</title>
		<link>http://aproxacs.blog.qrobo.com/2009/07/08/creating-msn-messenger-bot-with-ruby/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/07/08/creating-msn-messenger-bot-with-ruby/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 22:37:30 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[messenger]]></category>
		<category><![CDATA[msn]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/07/08/creating-msn-messenger-bot-with-ruby/</guid>
		<description><![CDATA[




If you are willing to make MSN messenger bot program with ruby, there is a good libray, rubymsn. Though it doesn’t seem to be active now, you can download sources and use them. Throughout this article, I will introduce rubymsn library to share you what I’ve learned. 
First, you can refer to MSN messenger protocol [...]]]></description>
			<content:encoded><![CDATA[<!-- Easy AdSense V2.41 -->
<!-- Post[count: 3] -->
<div class="ezAdsense adsense adsense-leadin" style="text-align:center;margin:12px;"><script type="text/javascript"><!--
google_ad_client = "pub-5569204050478966";
/* 468x60, 작성됨 09. 3. 1 */
google_ad_slot = "7973811031";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>If you are willing to make MSN messenger bot program with ruby, there is a good libray, <a href="http://code.google.com/p/rubymsn/">rubymsn</a>. Though it doesn’t seem to be active now, you can download sources and use them. Throughout this article, I will introduce rubymsn library to share you what I’ve learned. </p>
<p>First, you can refer to <a href="http://www.hypothetic.org/docs/msn/index.php">MSN messenger protocol page</a>.</p>
<h4>Login to MSN messenger</h4>
<p>Just create MSNConnection with MSN messenger id and password.</p>
<pre>@conn = MSNConnection.new(msn[:id], msn[:password])
@conn.signed_in = lambda {
  @conn.change_nickname &quot;Twitter Bot&quot;
}</pre>
<p>signed_in proc will be executed after successful login. Used change_nickname to change nickname as ‘Twitter Bot’. It does not do anything untils @conn.start is called, though.</p>
<h4>Receiving a message and Reply</h4>
<p>Use new_chat_session proc to indicate what to do when the session between the bot and a user is created.</p>
<pre>@conn.new_chat_session = lambda do |tag, session|
  TwitterBot.info &quot;=&gt; new chat session created with tag '#{tag}'!&quot;

  session.message_received = lambda do |sender, message|
    TwitterBot.info &quot;=&gt; #{sender} says: #{message}&quot;
    session.say &quot;hello&quot;
  end

  session.start
end
@conn.start</pre>
<p>message_received proc is called when the message from a user arrives. Possible procs for the session are&#160; :message_received, :session_started, :session_ended, and :participants_updated<br />
  <br />To reply or send a message to the user, use session.say.&#160; <br />The&#160; tag is used to a key to access the users’ session. A session is stored @conn.chatsessions as a hash. So a session can be retrieved like below.</p>
<pre>session = @conn.chatsessions[tag]</pre>
<h4>How to know the state of friends.</h4>
<p>A user has several states depending on his connection such as Online, Offline, Busy, etc. The state of user can be checked with @conn.contactlists. The following is how to know whether a user is offline.</p>
<pre>def offline?(email)
  contact = @conn.contactlists[&quot;FL&quot;][email]
  contact ? contact.status.code == &quot;FLN&quot; : true
end</pre>
<h4>Sending a message to the user</h4>
<p>In order to send a message to the friend, you have to get or create the session first. To create a session, several handshake messages are needed. This handshake is triggered by @conn.start_chat method.</p>
<pre>@conn.start_chat(tag, email)</pre>
<p>A tag can be anything unique. An email is the email address of the user to chat. The session is not created immediately, it takes some time until the handshake finished. How to know when the session is created? A session_started proc is called as soon as the session is created.</p>
<pre>@conn.new_chat_session = lambda do |tag, session|
  ...
  session.session_started = lambda do
    msg = get_message(tag) # assume that a message is possible to get with tag.
    session.say msg
    session.close
  end
  session.start
end</pre>
<p>When the session is started, A message is sent. Above code assumes that a message is stored before and can be retrieved with a tag.</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/07/08/creating-msn-messenger-bot-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing url parameter with javascript.</title>
		<link>http://aproxacs.blog.qrobo.com/2009/07/04/changing-url-parameter-with-javascript/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/07/04/changing-url-parameter-with-javascript/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:08:11 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[regular]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/07/04/changing-url-parameter-with-javascript/</guid>
		<description><![CDATA[Here is a url. “http://localhost/service?page=2”. What I want to do is to add ‘per_page’ parameter to the url. If I can convince that the url does not have ‘per_page’ parameter, itt’s easy. just add the ‘&#38;per_page=20’ to the end of url. But there are several patterns in url, and I want to handle all the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a url. “http://localhost/service?page=2”. What I want to do is to add ‘per_page’ parameter to the url. If I can convince that the url does not have ‘per_page’ parameter, itt’s easy. just add the ‘&amp;per_page=20’ to the end of url. But there are several patterns in url, and I want to handle all the cases well.</p>
<p>The patterns are like belows.</p>
<pre>1. http://localhost/service
2. http://localhost/service?page=2
3. http://localhost/service?per_page=2
4. http://localhost/service?page=2&amp;per_page=10
5. http://localhost/service?per_page=10&amp;page=2
6. http://localhost/service?page=2&amp;per_page=10&amp;sort=name</pre>
<p>1. No parameter,<br />
  <br />2. per_page does not exist, </p>
<p>3. only per_page exists, </p>
<p>4. per_page is at the end, </p>
<p>5. per_page is at the first, </p>
<p>6. per_page is in the middle. </p>
<p>After a couple of hours of consideration, I decided to use a regular expression. My strategy is simple. Remove per_page param first, and then add per_page param.</p>
<pre>function update_param(url, value) {
 return url.replace(/per_page=\d+&amp;?/, &quot;&quot;)+&quot;&amp;per_page=&quot;+value;
}</pre>
<p>Very simple. It searches ‘per_page=xxx’ or ‘per_page=xxx&amp;’ pattern from url and remove it. And then add per_page param to the end. But it’s not working for every case, unforunately. It does not return the good result for case 1 and 3. And it assumes that the value of per_page is number.</p>
<p>My final solution is the below.</p>
<pre>function update_param(url, name, value) {
  host = url.split(&quot;?&quot;)[0];
  params = url.split(&quot;?&quot;)[1] || &quot;&quot;;
  var re1 = new RegExp(name + '=.+&amp;');
  var re2 = new RegExp(name + '=.+');
  params = params.replace(re1, &quot;&quot;).replace(re2, &quot;&quot;).replace(/&amp;$/, &quot;&quot;)+&quot;&amp;&quot;+name+&quot;=&quot;+value;
  params = params.replace(/^&amp;/, &quot;&quot;);

  return host + &quot;?&quot; + params;
}</pre>
<p>Now it works for every cases. Moreover, it receives param name as a parameter, and it does not assume the value’s type. But more complicated than before.</p>
<p>I am using regular expressions more and more nowdays. Even though It seemed very difficult for the first time, I’ve come to know it wonderful, as I’ve studied more and more. My final solution does not seem to be good. It’s too complicated. Let me konw your opinion to make it better.</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/07/04/changing-url-parameter-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cattr_accessor and mattr_accessor</title>
		<link>http://aproxacs.blog.qrobo.com/2009/07/03/cattr_accessor-and-mattr_accessor/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/07/03/cattr_accessor-and-mattr_accessor/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 13:22:42 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[cattr]]></category>
		<category><![CDATA[mattr]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/07/03/cattr_accessor-and-mattr_accessor/</guid>
		<description><![CDATA[In ruby, there is no public variable. All variables in object are hidden encapsulated. To access objects’ variables, getter and setter methods are necessary. It may sound painful if I have to make all the getter/setter methods. But don’t worry. Ruby has a smart solution. attr_accessor. 
attr_accessor :school
attr_reader :student
attr_writer :student
This is good. With a simple [...]]]></description>
			<content:encoded><![CDATA[<p>In ruby, there is no public variable. All variables in object are hidden encapsulated. To access objects’ variables, getter and setter methods are necessary. It may sound painful if I have to make all the getter/setter methods. But don’t worry. Ruby has a smart solution. <strong>attr_accessor</strong>. </p>
<pre>attr_accessor :school
attr_reader :student
attr_writer :student</pre>
<p>This is good. With a simple declaration, encapsulation is achieved. It is OK for instance variables, but what about class variables? Unfortunately, ruby core does not have the attr_xxx for class variables. However, It is not that difficult to implement. </p>
<pre>class Class
  def cattr_accessor(name)
    class_eval &lt;&lt;-EOS
        unless defined? @@#{name}
          @@#{name} = nil
        end                      

        def self.#{name}
          @@#{name}
        end                      

        def self.#{name}=(obj)
          @@#{name} = obj
        end
    EOS
  end
end

class Test
  cattr_accessor :greet
end

Test.greet = &quot;hello&quot;
p Test.greet</pre>
<h4>ActiveSupport</h4>
<p>There is a easier way if you set up your mind to use activesupport. Activesupport already implemented <strong>cattr_xxx</strong> and <strong>mattr_xxx</strong> serieses.</p>
<pre>require &quot;activesupport&quot;
class ClassTest
  cattr_accessor :greet
end
module ModuleTest
  mattr_accessor :greet
end </pre>
<p>cattr_xxx works for class variables, and mattr_xxx wors for module variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/07/03/cattr_accessor-and-mattr_accessor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passenger and SE linux</title>
		<link>http://aproxacs.blog.qrobo.com/2009/07/03/passenger-and-se-linux/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/07/03/passenger-and-se-linux/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 15:43:23 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[selinux]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/07/03/passenger-and-se-linux/</guid>
		<description><![CDATA[Before passenger, it was a big issue to deploy rails appliation. We needed so many things to configure such as mogrel, mogrel cluster, and god. Now we just need a passenger and apache. It is as simple as PHP. What makes me more impressed is passenger is super easy to install. It uses gem system.
sudo [...]]]></description>
			<content:encoded><![CDATA[<p>Before passenger, it was a big issue to deploy rails appliation. We needed so many things to configure such as mogrel, mogrel cluster, and god. Now we just need a passenger and apache. It is as simple as PHP. What makes me more impressed is passenger is super easy to install. It uses gem system.</p>
<pre>sudo gem install passenger</pre>
<p>To install passenger as a apache module passenger-install-apache2-module command is used.</p>
<pre>passenger-install-apache2-module</pre>
<p>This command is very nice because it notifies me what to do next whenever there is a problem. And It shows apache configuration to set at the end. How nice!</p>
<p><a href="http://blog.aproxacs.com/files/2009/07/clip-image001.png"><img border="0" alt="clip_image001" src="http://blog.aproxacs.com/files/2009/07/clip-image001-thumb.png" width="628" height="501" /></a></p>
<h4>SE linux</h4>
<p>A week ago, I asked to set up a deploy environment with passenger and apache on fedora linux. I thought it would be a piece of cake at the first time, but I struggled all day. I installed passenger and configure apache settings and virtual hosts as always. After restarting apache, I found it failed. What’s wrong?</p>
<p>The folowing error occured. This error can be checked in <strong>/var/log/httpd/error_log</strong> file.</p>
<pre>[Thu Jul 02 17:53:03 2009] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Thu Jul 02 17:53:03 2009] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
*** Passenger ERROR (ext/common/ApplicationPoolServer.h:643):
Cannot execute /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/ApplicationPoolServerExecutable: Permission denied (13)</pre>
<p>A permission to execute passenger is denied. But permission is allowed to everybody if I check this file by ls command.</p>
<pre>-rwxr-xr-x. 1 root root 2528880 2009-07-02 17:35 /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/ApplicationPoolServerExecutable</pre>
<p><strong>This is thanks to the </strong><a href="http://en.wikipedia.org/wiki/Selinux"><strong>SELinux</strong></a><strong> policy</strong>. SELinux is Secure Enhanced linux. I am not explainig SE linux here. One thing I know is httpd is affected by the SE linux policy and this policy prevents passenger from executing. The easist way to avoid this is not to use SE Linux. One time command is </p>
<pre>setenforce Permissive</pre>
<p>If you want not to use it permanently, edit <strong>/etc/selinux/config</strong></p>
<pre>SELINUX=permissive</pre>
<p>If you have to keep using SE linux, there is <a href="http://www.modrails.com/documentation/Users%20guide.html#_the_apache_error_log_says_that_the_spawn_manager_script_does_not_exist_or_that_it_does_not_have_permission_to_execute_it">how-to in passenger documents</a>. I followed these steps once, but failed. It kept showing errors that it did not have a permission to create directory or sock. I stopped a little quickly, casue it was not that important to me. Wish you have a good luck and share me if you success.</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/07/03/passenger-and-se-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to sign in with PIN-based twitter using oauth gem</title>
		<link>http://aproxacs.blog.qrobo.com/2009/06/28/how-to-sign-in-with-pin-based-twitter-using-oauth-gem/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/06/28/how-to-sign-in-with-pin-based-twitter-using-oauth-gem/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 11:45:10 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[pin]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/06/28/how-to-sign-in-with-pin-based-twitter-using-oauth-gem/</guid>
		<description><![CDATA[Twitter uses PIN-based oauth to the desktop applications. This document explains how it works. All things are same as normal oauth procedure except PIN. After finishing authorization with Twitter, user sees 6 digit PIN number on twitter. This PIN number has to be included to the access_token request with oauth_verifier parameter to get access token [...]]]></description>
			<content:encoded><![CDATA[<p>Twitter uses PIN-based oauth to the desktop applications. <a href="http://apiwiki.twitter.com/Authentication">This document</a> explains how it works. All things are same as normal oauth procedure except PIN. After finishing authorization with Twitter, user sees 6 digit PIN number on twitter. This PIN number has to be included to the access_token request with oauth_verifier parameter to get access token successfully. </p>
<p><a href="http://oauth.rubyforge.org/">oauth gem</a>&#160; is a great tool if you inted to use oauth with ruby. This is how to send oauth_verifier parameter with oauth gem. Until version 0.3.4, oauth_verifier parameter has to be included in the body.</p>
<pre>acc_token = req_token.get_access_token({}, {:oauth_verifier =&gt; pin})</pre>
<p>From version 0.3.5, oauth gem has the oauth_verifier in oauth parameters. So the below is also possible.</p>
<pre>acc_token = req_token.get_access_token({:oauth_verifier =&gt; pin})</pre>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/06/28/how-to-sign-in-with-pin-based-twitter-using-oauth-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using delegate in ruby</title>
		<link>http://aproxacs.blog.qrobo.com/2009/06/28/using-delegate-in-ruby/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/06/28/using-delegate-in-ruby/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 00:26:54 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[forwardable]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/06/28/using-delegate-in-ruby/</guid>
		<description><![CDATA[As I have been wandering ruby gems and rails plugin codes, I found delegate is used in many ways. For example, in attachment_fu plugin,&#160; 
delegate :content_types, :to =&#62; Technoweenie::AttachmentFu
And in mechanize gem, 
def_delegator :parser, :search, :search
Things to do with above codes are intuitive. In the first, if it receives content_types request, then invoke Technoweenie::AttachementFu.content_types. And [...]]]></description>
			<content:encoded><![CDATA[<p>As I have been wandering ruby gems and rails plugin codes, I found delegate is used in many ways. For example, in attachment_fu plugin,&#160; </p>
<pre>delegate :content_types, :to =&gt; Technoweenie::AttachmentFu</pre>
<p>And in mechanize gem, </p>
<pre>def_delegator :parser, :search, :search</pre>
<p>Things to do with above codes are intuitive. In the first, if it receives content_types request, then invoke Technoweenie::AttachementFu.content_types. And in second one, if it receives search request, then invoke parser.search method. It is what the delegate normally do.<br />
  <br />This is handy beacause it reduces the effort to declare an additional function like below.</p>
<pre>def content_types
  Technoweenie::AttachmentFu.content_types
end</pre>
<p>As you can see in the above examples, there are two ways in using delegate.(Maybe more than two in the whole ruby world, but now I am focusing what I’ve found.) One is using activesupport, and the other way is using <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html">Forwardable</a> module in <a href="http://www.ruby-doc.org/stdlib/">Ruby standard libray</a>. </p>
<h4>delegate in ActiveSupport</h4>
<p>Activesupport extends Module and adds delegate method to it. Therefore just by requiring activesupport, you can use delegate method at any Class.</p>
<pre>irb(main):001:0&gt; Object.respond_to? :delegate
=&gt; false
irb(main):002:0&gt; require 'activesupport'
=&gt; true
irb(main):003:0&gt; Object.respond_to? :delegate
=&gt; true</pre>
<p>Using delegate is easy. Just two things are needed. method name to delegate and target object.</p>
<pre>require 'activesupport'
class SayHello
  def greet(name)
    &quot;hello #{name}&quot;
  end
end

class Foo
  attr_accessor :greeter
  <strong>delegate :greet, :to =&gt; :greeter</strong>
end

foo = Foo.new
foo.greeter = SayHello.new
foo.greet(&quot;Tom&quot;)&#160; # =&gt; &quot;hello Tom&quot;</pre>
<p>After declaring delegate, Foo comes to have instance method ‘greet’, which forwards greet request to greeter. To make delegate smarter, two more options, <strong>prefix</strong> and <strong>allow_nil </strong>are possible.</p>
<p>A prefix option adds prefix word to the method. For example ,</p>
<pre>class Foo
  attr_accessor :greeter
  <strong>delegate :greet, :to =&gt; :greeter, :prefix =&gt; :hello</strong>
end
foo.<strong>hello_greet</strong>(&quot;Tom&quot;)  # =&gt; &quot;hello Tom&quot;</pre>
<p>If allow_nil option is true, delegate method returns nil instead of Exception, when delegate object is absent. For example,</p>
<pre>class Foo
  attr_accessor :greeter
  <strong>delegate :greet, :to =&gt; :greeter, :allow_nil =&gt; true</strong>
end
foo = Foo.new
foo.greeter = nil
<strong>foo.greet(&quot;Tom&quot;)&#160; # =&gt; nil</strong></pre>
<h4>delegate in Forwardable module</h4>
<p>If you are not a big fan of activesupport, you can use <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html">Forwardable</a> module in standard ruby library. To do the same thing using forwardable is like below.</p>
<pre><strong>require &quot;forwardable&quot;</strong>
class SayHello
  def greet(name)
    &quot;hello #{name}&quot;
  end
end

class Foo
  <strong>extend Forwardable</strong>
  attr_accessor :greeter
  <strong>def_delegator :greeter, :greet</strong>
end

foo = Foo.new
foo.greeter = SayHello.new
foo.greet(&quot;Tom&quot;)  # =&gt; &quot;hello Tom&quot; </pre>
<p>The parameters of <strong>def_delegator</strong> is <strong>target_object, method, alias_method</strong>(optional). An alias_method is the name of method that Foo creates. If alias_method is absent, method is used instead. So In the above, Foo come to have greet method, which calls greeter.greet. Any other options are not allowed.</p>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/06/28/using-delegate-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenStruct instead of Hash</title>
		<link>http://aproxacs.blog.qrobo.com/2009/06/23/openstruct-instead-of-hash/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/06/23/openstruct-instead-of-hash/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 22:31:38 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[ostruct]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/06/23/openstruct-instead-of-hash/</guid>
		<description><![CDATA[Hash is a great way to represent the data structure. It is easily converted to xml, json and yaml. Activesupport extends core Hash class and enables these methods.    
hash = Hash.from_xml(xml)
hash = Hash.from_json(xml)

  Very convenient. But “[ ]” notation to access hash value is sometimes anoying. I want to use “.” [...]]]></description>
			<content:encoded><![CDATA[<p>Hash is a great way to represent the data structure. It is easily converted to xml, json and yaml. Activesupport extends core Hash class and enables these methods.    </p>
<pre lang="ruby">hash = Hash.from_xml(xml)
hash = Hash.from_json(xml)</pre>
<p>
  <br />Very convenient. But “[ ]” notation to access hash value is sometimes anoying. I want to use “.” operation instead of “[ ]”. One solution is to make a Class and use method_missing. </p>
<p></p>
<pre>class Box
  def initialize(data)
    @data = data.symbolize_keys
  end
  def method_missing(method, *args)
    return @data[method] if @data.has_key?(method)
    super
  end
end
data = {&quot;box&quot; =&gt; {&quot;name&quot; =&gt; &quot;creative&quot;, &quot;user&quot; =&gt; {&quot;name&quot; =&gt; &quot;Tom&quot;}}}
box = Box.new(data[&quot;box&quot;])
box.name</pre>
<p>
  <br />It works, but needs many codes like Box class. <a href="http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/index.html">OpenStruct</a> is an easier way to do the same thing. </p>
<p></p>
<pre>require 'ostruct'
data = {'box' =&gt; {&quot;name&quot; =&gt; &quot;creative&quot;, &quot;user&quot; =&gt; {&quot;name&quot; =&gt; &quot;Tom&quot;}}}
box = OpenStruct.new(data[&quot;box&quot;])
box.name</pre>
<p>
  <br />Internally OpenStruct uses hash. And it creates methods of hash’s key. For example box has methods “name” and “name=”. </p>
<p></p>
<pre>box.public_methods.include? &quot;name&quot;
=&gt; true
box.public_methods.include? &quot;name=&quot;
=&gt; true</pre>
<p>
  <br />One thing insufficient of OpenStruct is it only converts 1 dimension of hash. So the box.user.name is not possible. </p>
<p></p>
<pre>box.user
=&gt; {&quot;name&quot;=&gt;&quot;Tom&quot;}</pre>
<p>
  <br />In order to use box.user.name, nested converting is needed. </p>
<p></p>
<pre>def to_openstruct(hash)
  hash.inject(OpenStruct.new) { |result, (key, value)|
    value = to_openstruct(value) if value.is_a? Hash
    result.send(&quot;#{key}=&quot;, value)
    result
  }
end
data = {&quot;box&quot; =&gt; {&quot;name&quot; =&gt; &quot;creative&quot;, &quot;user&quot; =&gt; {&quot;name&quot; =&gt; &quot;Tom&quot;}}}
box = to_openstruct(data[&quot;box&quot;])
box.user.name</pre>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/06/23/openstruct-instead-of-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using jQuery.data() function to store data associated with the element</title>
		<link>http://aproxacs.blog.qrobo.com/2009/06/22/using-jquerydata-function-to-store-data-associated-with-the-element/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/06/22/using-jquerydata-function-to-store-data-associated-with-the-element/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 23:09:39 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.aproxacs.com/2009/06/22/using-jquerydata-function-to-store-data-associated-with-the-element/</guid>
		<description><![CDATA[One easy way to store element specific data is to use the attribute of HTML element like below.
$(“div#ID”).attr(“data”, “id:3”)

This works. But it doesn’s seem very nice for some reasons. First, it uses ‘data’ attribute, which is not valid XHTML. It contaminates HTML document. Second, it is hard to parse data. More complicate data, harder to [...]]]></description>
			<content:encoded><![CDATA[<p>One easy way to store element specific data is to use the attribute of HTML element like below.</p>
<pre lang="javascript">$(“div#ID”).attr(“data”, “id:3”)</pre>
<p><strike></strike></p>
<p>This works. But it doesn’s seem very nice for some reasons. First, it uses ‘data’ attribute, which is not valid XHTML. It contaminates HTML document. Second, it is hard to parse data. More complicate data, harder to parse. You can use json string as data to ease parsing, but eval operation is expensive.</p>
<p>There is an absolutely better way if you’re using jquery. data function.</p>
<pre lang="javascript">// save
var user = {id:3, name='clever'}
$(“div#ID”).data(“user”, user)
// get
user = $(“div#ID”).data(“user”)
// delete
$(“div#ID”).removeData(“user”)</pre>
<p>This is much easier to understand and fast. Moreover, firebug does not show any of data, cause it does not use any HTML attributes. How nice! Actually, it uses internal cache to store data.</p>
<p>Lets’s inspect jquery source codes to understand how jQuery.data works. All data is stored in jQuery.cache. Element can access its data using cache_id, which is just increasing number. The Element only stores this cache_id with the name of ‘jQuery’ + Timestamp. So the process to find element’s data is like below.</p>
<ol>
<li>Find cache_id of the element. </li>
<li>Find data in cache using cache_id </li>
</ol>
<p>One nice additional feature using jQuery.data is queue. You can queue and dequeue jobs like below.</p>
<pre lang="javascript">
$("div#ID").queue("msg", function() { alert("progressing");});
$("div#ID").queue("msg", function() { alert("Done");});
// In worker
$("div#ID").queue("msg");</pre>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/06/22/using-jquerydata-function-to-store-data-associated-with-the-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[C#] using region to make codes organized</title>
		<link>http://aproxacs.blog.qrobo.com/2009/06/08/c-using-region-to-make-codes-organized/</link>
		<comments>http://aproxacs.blog.qrobo.com/2009/06/08/c-using-region-to-make-codes-organized/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 02:00:05 +0000</pubDate>
		<dc:creator>aproxacs</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[region]]></category>

		<guid isPermaLink="false">http://aproxacs.blog.qrobo.com/2009/06/08/c-using-region-to-make-codes-organized/</guid>
		<description><![CDATA[It’s hard to read codes if there are many methods and properties in the class. One way to ease the complexity is using comments like ‘here comes the public methods’. But not working well. There is a better way if you’re woking with C#. Using region. 
#region makes it possible to bunddle asscociated codes. It [...]]]></description>
			<content:encoded><![CDATA[<p>It’s hard to read codes if there are many methods and properties in the class. One way to ease the complexity is using comments like ‘here comes the public methods’. But not working well. There is a better way if you’re woking with C#. Using region. </p>
<p>#region makes it possible to bunddle asscociated codes. It does nothing except having codes block. </p>
<p>A good thing to use #region is you can use ‘outlining and hiding code’ funciton of visual studio. It is convinient when reading very complex codes.</p>
<pre lang="csharp">class Region
{
  #region Fiels and Properties
  ...
  #endregion     

  #region Constructors
  ...
  #endregion     

  #region Public Methods
  #endregion     

  #region Private Method
  #endregion
}</pre>
<div style="margin-top: 10px;height: 15px" class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/5af1279d-25bd-4336-9e71-1d7d017b9c86/"><img style="float: right" class="zemanta-pixie-img" alt="Reblog this post [with Zemanta]" src="http://img.zemanta.com/reblog_e.png?x-id=5af1279d-25bd-4336-9e71-1d7d017b9c86" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://aproxacs.blog.qrobo.com/2009/06/08/c-using-region-to-make-codes-organized/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
