<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Domon</title>
  <link href="https://domon.cc/atom.xml" ref="self"/>
  <link href="https://domon.cc/"/>
  <updated>2021-05-01T06:33:03+00:00</updated>
  <id>https://domon.cc/</id>
  <author>
    <name>Chun-wei Kuo</name>
  </author>

  
  <entry>
    <title>Null-safe Comparison in SQL</title>
    <link href="https://domon.cc/2018/01/30/null-safe-comparison-in-sql/"/>
    <updated>2018-01-30T00:00:00+00:00</updated>
    <id>https://domon.cc/2018/01/30/null-safe-comparison-in-sql</id>
    <content type="html">&lt;p&gt;Even though I know the concept of 3-valued logic in SQL, I trip over it from time to time.&lt;/p&gt;

&lt;p&gt;For example, say, we have some &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tag&lt;/code&gt;s and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tag&lt;/code&gt;s belong to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tag_group&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags&lt;/code&gt; table in MySQL might look like the following.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CREATE TABLE tags (
  id           BIGINT NOT NULL AUTO_INCREMENT,
  name         VARCHAR(20),
  tag_group_id BIGINT,

  PRIMARY KEY (id)
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Imagine we have the following rows in the table.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;INSERT INTO tags (name,      tag_group_id)
          VALUES ('mysql',   1),
                 ('psql',    1),
                 ('sqlite',  1),
                 ('android', 2),
                 ('ios',     2),
                 ('emacs',   NULL);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, let’s try to answer the following questions.&lt;/p&gt;

&lt;p&gt;How many tags in the table?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT count(*) FROM tags; -- 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;How many tags in tag group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT count(*) FROM tags WHERE tag_group_id = 1; -- 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;How many tags that are &lt;strong&gt;NOT&lt;/strong&gt; in tag group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT count(*) FROM tags WHERE tag_group_id != 1; -- 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wait… Isn’t the answer &lt;strong&gt;3&lt;/strong&gt; instead of 2?&lt;/p&gt;

&lt;p&gt;Actually, the result set returned by the query does not include the tag whose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tag_group_id&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is because &lt;strong&gt;in SQL, the equality between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt; and anything else is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt;, instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRUE&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALSE&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT        1 = 1,     -- 1
           NULL = NULL,  -- NULL (!)
              1 = NULL,  -- NULL (!)
       NOT(   1 = 1),    -- 0
       NOT(NULL = NULL), -- NULL (!)
       NOT(   1 = NULL), -- NULL (!)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Fortunately, there are so-called &lt;strong&gt;null-safe operators&lt;/strong&gt; which can perform equality comparison in a way that is common in many other programming languages.&lt;/p&gt;

&lt;p&gt;In MySQL, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;=&amp;gt;&lt;/code&gt; is a null-safe operator for equality.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT        1 &amp;lt;=&amp;gt; 1,     -- 1
           NULL &amp;lt;=&amp;gt; NULL,  -- 1
              1 &amp;lt;=&amp;gt; NULL,  -- 0
       NOT(   1 &amp;lt;=&amp;gt; 1),    -- 0
       NOT(NULL &amp;lt;=&amp;gt; NULL), -- 0
       NOT(   1 &amp;lt;=&amp;gt; NULL); -- 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;PostgreSQL provides &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IS DISTINCT FROM&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IS&lt;/code&gt; is the counterpart in SQLite.&lt;/p&gt;

&lt;p&gt;Finally, I have written this down. I hope this post can save me a few Google searches in the future. 💦&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>Private Class Methods in Ruby</title>
    <link href="https://domon.cc/2013/12/25/private-class-methods-in-ruby/"/>
    <updated>2013-12-25T00:00:00+00:00</updated>
    <id>https://domon.cc/2013/12/25/private-class-methods-in-ruby</id>
    <content type="html">&lt;p&gt;To make a class method private, the intuitive way is to put &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt;
before the method definition like the following.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Foo

  private

  def self.bar
    &quot;bar&quot;
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But that does not seem to be sufficient, unfortunately.&lt;/p&gt;

&lt;p&gt;The method is still public:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pry(main)&amp;gt; Foo.bar
=&amp;gt; &quot;bar&quot; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Actually, to make it private, we have to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private_class_method&lt;/code&gt; like the
following.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Foo

  def self.bar
    &quot;bar&quot;
  end
  private_class_method :bar

end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can confirm that it works by calling the class method in a Ruby console.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pry(main)&amp;gt; Foo.bar
NoMethodError: private method `bar' called for Foo:Class
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Why is that?&lt;/p&gt;

&lt;p&gt;Before we jump into the explanation, please take a look at the following code,
which is another way to make a class method private.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Foo
  class &amp;lt;&amp;lt; self

    private

    def bar
      &quot;bar&quot;
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the example above, the singleton class is opened and the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bar&lt;/code&gt;
is defined as a private instance method on it.&lt;/p&gt;

&lt;p&gt;The syntax looks different but it works as same as the previous example.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pry(main)&amp;gt; Foo.bar
NoMethodError: private method `bar' called for Foo:Class
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby, as we know, &lt;strong&gt;classes are just objects&lt;/strong&gt;.
When we define a “class method” on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt;, we are just defining an
&lt;strong&gt;instance method&lt;/strong&gt; on the &lt;strong&gt;singleton class&lt;/strong&gt; of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt; object.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pry(main)&amp;gt; Foo.singleton_class.private_instance_methods(false)
=&amp;gt; [:bar]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; is not a special keyword but a method on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Module&lt;/code&gt;.
It is available on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class&lt;/code&gt; because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class.is_a? Module&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; gets called, it sets the visibility for subsequently methods
&lt;strong&gt;defined on the current object&lt;/strong&gt; to private.&lt;/p&gt;

&lt;p&gt;Therefore, to set the visibility of methods &lt;strong&gt;defined on the singleton class of
the current object&lt;/strong&gt;, we need another method.
That is where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Module#private_class_method&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;To summarize, there are two styles of syntax to create a private class method
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bar&lt;/code&gt; on a class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Define the method with the syntax &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def self.bar&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private_class_method :bar&lt;/code&gt; to make it private.&lt;/li&gt;
  &lt;li&gt;Or open the singleton class of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt; and define a private method on the singleton class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;p.s. The term “singleton class” referred in this post is also called “metaclass” or “eigenclass”.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>When Your Ctrl-C Gets Trapped</title>
    <link href="https://domon.cc/2013/07/14/when-your-ctrl-c-gets-trapped/"/>
    <updated>2013-07-14T00:00:00+00:00</updated>
    <id>https://domon.cc/2013/07/14/when-your-ctrl-c-gets-trapped</id>
    <content type="html">&lt;p&gt;Have you ever met a program that no matter how hard you hit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-C&lt;/code&gt;, it just wouldn’t stop?&lt;/p&gt;

&lt;p&gt;I was a little annoyed by such behavior of the &lt;a href=&quot;http://www.gnu.org/software/bc/&quot;&gt;bc&lt;/a&gt; (basic calculator) program on Mac (or FreeBSD):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ bc -q
(interrupt) use quit to exit.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In fact, when hitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-C&lt;/code&gt;, we are just sending a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGINT&lt;/code&gt; &lt;a href=&quot;https://en.wikipedia.org/wiki/Unix_signal&quot;&gt;signal&lt;/a&gt; to the program.
The program may &lt;a href=&quot;http://en.wikipedia.org/wiki/Trap_(computing)&quot;&gt;trap&lt;/a&gt; the signal and probably ignore it.&lt;/p&gt;

&lt;p&gt;To achieve that in Ruby is simple:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;loop do
  input = gets.chomp

  exit if input == 'quit'

  # calculate(input)

  trap(:INT) do
    puts '(interrupt) use quit to exit.'
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Some processes even wouldn’t stop when we try to &lt;a href=&quot;https://en.wikipedia.org/wiki/Kill_(command)&quot;&gt;kill&lt;/a&gt; it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kill &amp;lt;pid&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In such cases, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGTERM&lt;/code&gt; signal sent by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt; is trapped.&lt;/p&gt;

&lt;p&gt;We can force them to stop by sending the the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGKILL&lt;/code&gt; signal, since it cannot be caught:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kill -s KILL &amp;lt;pid&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
  </entry>
  
  <entry>
    <title>Shame Driven Development</title>
    <link href="https://domon.cc/2012/12/12/shame-driven-development/"/>
    <updated>2012-12-12T00:00:00+00:00</updated>
    <id>https://domon.cc/2012/12/12/shame-driven-development</id>
    <content type="html">&lt;p&gt;Several days ago, I saw a &lt;a href=&quot;https://codeiq.jp/ace/tsukada_shinnosuke/q129&quot;&gt;code challenge&lt;/a&gt; on &lt;a href=&quot;https://codeiq.jp/&quot;&gt;CodeIQ&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It challenges users to write a ruby program that mimics the basic functionalities of the UNIX &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt; command.
Users are encouraged to not rely on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Date&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Time&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Datetime&lt;/code&gt; or other libraries.
Expected time of writing this program is 15 minutes.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cal 12 2012
   December 2012
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That should be an easy exercise.
Almost, if not every, programmer has written a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt; or two in their early days of programming.
I should have written one in C in my first year of college.
But, could I write a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt; now, say, in 15 minutes? Without relying on any libraries?&lt;/p&gt;

&lt;p&gt;I pondered. Then a question flashed into my mind: How do I know if a year is a leap year or not?
In elementary school, I was taught that there is a leap year every 4 years.
Is it really as simple as that?&lt;/p&gt;

&lt;p&gt;As usual, Wikipedia has &lt;a href=&quot;http://en.wikipedia.org/wiki/Leap_year&quot;&gt;the answer&lt;/a&gt;.
And there is some pseudo code kindly listed:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if year modulo 400 is 0 then
   is_leap_year
else if year modulo 100 is 0 then
   not_leap_year
else if year modulo 4 is 0 then
   is_leap_year
else
   not_leap_year
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now I know that the year of 1900 (and 1800, 1700, …) was not a leap year:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cal 2 1900
   February 1900
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Furthermore, with some quick search on the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt;, I found a more surprising fact.
In the year 1752, there were no such days as September 3, 4, … till 13.
At least the command told me so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cal 9 1752
   September 1752
Su Mo Tu We Th Fr Sa
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(You can read more about the history &lt;a href=&quot;http://www.csd.uwo.ca/staff/magi/personal/humour/Computer_Audience/'cal%209%201752'%20explained.html&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Now, back to the title of this post.&lt;/p&gt;

&lt;p&gt;Actually, I have written &lt;a href=&quot;https://github.com/Domon/dcal&quot;&gt;a stupid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt;&lt;/a&gt;. (Not in 15 minutes, sorry.)
It takes month and year as the arguments, and outputs a calendar similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cal&lt;/code&gt;.
It does not take care of the years before 1970, and has no any other fancy features.&lt;/p&gt;

&lt;p&gt;Why do I bother putting a stupid program online?
Because I am so shameful about it.
And I feel that I have to accept it and do something to force me grow.&lt;/p&gt;

&lt;p&gt;By attacking problems continuously, I will learn and hopefully be able to write less shameful code.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>Equality of Active Record Objects</title>
    <link href="https://domon.cc/2012/05/25/equality-of-active-record-objects/"/>
    <updated>2012-05-25T00:00:00+00:00</updated>
    <id>https://domon.cc/2012/05/25/equality-of-active-record-objects</id>
    <content type="html">&lt;p&gt;There was once a caching problem in a project I am working on.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt; of some posts is used as cache keys. According to &lt;a href=&quot;http://ruby-doc.org/core-1.9.3/Object.html#method-i-hash&quot;&gt;the document&lt;/a&gt;, I expect &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object#hash&lt;/code&gt; will return different numbers if any attribute changes. But the cache just won’t refresh.&lt;/p&gt;

&lt;p&gt;To figure out that problem, let’s start with two posts (in rails console):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[1] pry(main)&amp;gt; p1 = Post.first
  Post Load (0.2ms)  SELECT &quot;posts&quot;.* FROM &quot;posts&quot; LIMIT 1
=&amp;gt; #&amp;lt;Post id: 4, title: &quot;Hello&quot;, content: &quot;World.&quot;, created_at: &quot;2012-05-20 08:38:46&quot;, updated_at: &quot;2012-05-20 08:38:46&quot;&amp;gt;
[2] pry(main)&amp;gt; p2 = Post.first
  Post Load (0.2ms)  SELECT &quot;posts&quot;.* FROM &quot;posts&quot; LIMIT 1
=&amp;gt; #&amp;lt;Post id: 4, title: &quot;Hello&quot;, content: &quot;World.&quot;, created_at: &quot;2012-05-20 08:38:46&quot;, updated_at: &quot;2012-05-20 08:38:46&quot;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;They are the same post:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[3] pry(main)&amp;gt; p1 == p2
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we change an attribute of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p1&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[4] pry(main)&amp;gt; p1.title = &quot;hi&quot;
=&amp;gt; &quot;hi&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p1&lt;/code&gt; still equals to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p2&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[5] pry(main)&amp;gt; p1 == p2
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;They also have the same hash:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[6] pry(main)&amp;gt; p1.hash == p2.hash
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But, why?&lt;/p&gt;

&lt;p&gt;Actually, the hash of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p1&lt;/code&gt; is the hash of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p1.id&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[7] pry(main)&amp;gt; p1.hash
=&amp;gt; -2154204912980276923
[8] pry(main)&amp;gt; p1.id.hash
=&amp;gt; -2154204912980276923
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can confirm that from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;activerecord/lib/active_record/core.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def ==(comparison_object)
  super ||
    comparison_object.instance_of?(self.class) &amp;amp;&amp;amp;
    id.present? &amp;amp;&amp;amp;
    comparison_object.id == id
end
alias :eql? :==

def hash
  id.hash
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That is, &lt;strong&gt;when we compare two Active Record objects, we are comparing their ids.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a cache key, you may consider using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cache_key&lt;/code&gt; method:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[9] pry(main)&amp;gt; p1.cache_key
=&amp;gt; &quot;posts/5-20120523014730&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It returns a string, which contains the model name, id, and updated_at timestamp of the record.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>A New Design</title>
    <link href="https://domon.cc/2012/04/22/a-new-design/"/>
    <updated>2012-04-22T00:00:00+00:00</updated>
    <id>https://domon.cc/2012/04/22/a-new-design</id>
    <content type="html">&lt;p&gt;Yesterday I was kind of fed up with the unpleasant design of my blog, and decided to do something about that.&lt;/p&gt;

&lt;p&gt;An attractive solution is migrating to &lt;a href=&quot;http://octopress.org/&quot;&gt;Octopress&lt;/a&gt;. But that way I could no longer rely on Github to build and host the pages for me.&lt;/p&gt;

&lt;p&gt;Then I found &lt;a href=&quot;http://jekyllbootstrap.com/&quot;&gt;Jekyll-Bootstrap&lt;/a&gt;, a simple yet powerful framework. Since it does not include any plugins, pages can still be built and hosted on Github. I was so excited and definitely tried it, but was not fully satisfied with its DOM structure.&lt;/p&gt;

&lt;p&gt;Of course I can customize JB to fit my needs, but suddenly I realized what I really need is just a new design. Although I am not a designer, I can utilize my newly learned knowledge of CSS and make it look better. And this is the result.&lt;/p&gt;

&lt;p&gt;Please feel free to give me any feedback or suggestions about this site. I’d be glad to hear your thoughts!&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>Version Control by Example</title>
    <link href="https://domon.cc/2011/09/28/version-control-by-example/"/>
    <updated>2011-09-28T00:00:00+00:00</updated>
    <id>https://domon.cc/2011/09/28/version-control-by-example</id>
    <content type="html">&lt;p&gt;This morning after I woke up, my dad told me that my book has arrived.&lt;/p&gt;

&lt;p&gt;“I opened the envelope because it was somewhat damp.” he explains.&lt;/p&gt;

&lt;p&gt;Damn postal service. But what book?&lt;/p&gt;

&lt;p&gt;The envelope from Chicaco almost answered my question. Yes, the book lying next to the envelope is &lt;a href=&quot;http://www.ericsink.com/&quot;&gt;Eric Sink&lt;/a&gt;’s &lt;a href=&quot;http://www.ericsink.com/vcbe/&quot;&gt;Version Control by Example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I requested a free copy of the book, and totally forgot about that. I didn’t expect such a nice book would be mailed overseas to my house, for free. But now the book is on my desk.&lt;/p&gt;

&lt;p&gt;Thank you, Eric!&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <title>Blogging like a hacker</title>
    <link href="https://domon.cc/2011/06/30/blogging-like-a-hacker/"/>
    <updated>2011-06-30T00:00:00+00:00</updated>
    <id>https://domon.cc/2011/06/30/blogging-like-a-hacker</id>
    <content type="html">&lt;p&gt;This is the very first post.&lt;/p&gt;

&lt;p&gt;Ever since reading “&lt;a href=&quot;http://tom.preston-werner.com/2008/11/17/blogging-like-a-hacker.html&quot;&gt;Blogging Like a Hacker&lt;/a&gt;” long ago, I’ve been wanting to own a similar site. No worries on database things. Writing articles in plain text feels me good.&lt;/p&gt;

&lt;p&gt;I also considered &lt;a href=&quot;http://nanoc.stoneship.org/&quot;&gt;nanoc&lt;/a&gt;, since it sounds so powerful. But I chose to learn the simpler &lt;a href=&quot;http://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt; first.&lt;/p&gt;

&lt;p&gt;After setting up this site, I found the existence of &lt;a href=&quot;http://octopress.org/&quot;&gt;Octopress&lt;/a&gt;, a helpful framework based on Jekyll. It features a semantic HTML5 template, Disqus Comments support, beautiful syntax highlightings, and many more. Using it would surely make the building process easier. I wish I could know this earlier, but I also learned a lot through building the site from scratch.&lt;/p&gt;

&lt;p&gt;There’s still a lot more to do, but right now I’d like to call it a day.&lt;/p&gt;

</content>
  </entry>
  

</feed>
