<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Shugo's Blog</title>
    <link>http://blog.shugo.net/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Shugo's Blog</description>
    <item>
      <title>FizzBuzz with Sather like loop</title>
      <description>&lt;pre&gt;&lt;code&gt;num = (1..100).each
fizz = ([""] * 2 + ["Fizz"]).cycle
buzz = ([""] * 4 + ["Buzz"]).cycle
loop do
  n = num.next
  puts (fizz.next + buzz.next).sub(/\A\z/) { n }
end
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Sat, 25 Aug 2007 08:06:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2007/08/25/fizzbuzz-with-sather-like-loop</guid>
      <link>http://blog.shugo.net/articles/2007/08/25/fizzbuzz-with-sather-like-loop</link>
      <category>Ruby</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/25</trackback:ping>
    </item>
    <item>
      <title>Graceful Restart with mod_fcgid</title>
      <description>&lt;p&gt;I tried to restart Rails application gracefully (apachectl graceful) with mod_fcgid, but I couldn't get a response on graceful restart.&lt;/p&gt;

&lt;p&gt;The problems are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mod_fcgid sends SIGTERM to FastCGI processes on graceful restart, but Rails FastCGI processes exit as soon as it's received SIGTERM (Rails FastCGI processes stop gracefully on SIGUSR1, not SIGTERM).&lt;/li&gt;
&lt;li&gt;Apache child processes exit on SIGUSR1 with mod_fcgid, as soon as it's received a signal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I created &lt;a href="http://shugo.net/tmp/mod_fcgid.2.0-graceful.diff"&gt;a patch&lt;/a&gt;
to fix these problems, but I'm not sure....&lt;/p&gt;</description>
      <pubDate>Sat, 25 Aug 2007 00:39:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2007/08/25/graceful-restart-with-mod_fcgid</guid>
      <link>http://blog.shugo.net/articles/2007/08/25/graceful-restart-with-mod_fcgid</link>
      <category>Rails</category>
      <category>mod_fcgid</category>
      <category>Apache</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/24</trackback:ping>
    </item>
    <item>
      <title>Sather like loop</title>
      <description>&lt;p&gt;Sather like loop is available in the SVN head.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;e1 = [1,2,3,4,5].each
e2 = ["a","b","c","d","e"].each

loop do
  p [e1.next, e2.next]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's cool, isn't it?&lt;/p&gt;</description>
      <pubDate>Fri, 24 Aug 2007 22:15:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2007/08/24/sather-like-loop</guid>
      <link>http://blog.shugo.net/articles/2007/08/24/sather-like-loop</link>
      <category>Ruby</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/23</trackback:ping>
    </item>
    <item>
      <title>Building MySQL/Ruby on mswin32</title>
      <description>&lt;p&gt;I created a &lt;a href="http://shugo.net/tmp/mysql-ruby-2.7.1.diff"&gt;patch&lt;/a&gt;
 to build MySQL/Ruby on mswin32.
With this patch, just type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; ruby extconf.rb --with-opt-include=../mysql-4.1.21-win32/include \
                  --with-opt-lib=../mysql-4.1.21-win32/lib/opt
&amp;gt; nmake
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you'll get mysql.so. Really easy.&lt;/p&gt;

&lt;p&gt;If libmysql.dll is built by VC7 or later, there may be a problem with GC.
It depends on whether MySQL/Ruby releases memory allocated by libmysql.dll using free()/xfree() or not.&lt;/p&gt;</description>
      <pubDate>Mon, 25 Sep 2006 06:30:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/09/25/building-mysql-ruby-on-mswin32</guid>
      <link>http://blog.shugo.net/articles/2006/09/25/building-mysql-ruby-on-mswin32</link>
      <category>Ruby</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/22</trackback:ping>
    </item>
    <item>
      <title>to_json hack for Safari</title>
      <description>&lt;p&gt;I found the reason why multibyte characters are encoded in to_json. Safari can't handle UTF-8 strings in text/javascript correctly:(&lt;/p&gt;

&lt;p&gt;Here is new code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class String
  JSON_ESCAPED = {
     "\010" =&amp;gt;  '\b',
     "\f" =&amp;gt;    '\f',
     "\n" =&amp;gt;    '\n',
     "\r" =&amp;gt;    '\r',
     "\t" =&amp;gt;    '\t',
     '"' =&amp;gt;     '\"',
     '\\' =&amp;gt;    '\\\\'
  }

  def to_json
    return '"' + gsub(/[\010\f\n\r\t"\\]/) { |s|
      JSON_ESCAPED[s]
    }.gsub(/([\xC0-\xDF][\x80-\xBF]|
             [\xE0-\xEF][\x80-\xBF]{2}|
             [\xF0-\xF7][\x80-\xBF]{3})+/ux) { |s|
      s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&amp;amp;')
    } + '"'
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I created a patch for ActiveSupport (trac of Rails is down, so I can't attach this patch to the ticket).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://shugo.net/tmp/to_json-2.diff"&gt;to_json-2.diff&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Sat, 15 Jul 2006 17:30:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/07/15/to_json-hack-for-safari</guid>
      <link>http://blog.shugo.net/articles/2006/07/15/to_json-hack-for-safari</link>
      <category>Rails</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/21</trackback:ping>
    </item>
    <item>
      <title>to_json too slow</title>
      <description>&lt;p&gt;I felt my application slow yesterday and profiled it.
RJS is very useful, but it seems to be slow.&lt;/p&gt;

&lt;p&gt;Thanks to Charlies's ruby-prof (it doesn't belongs to me any more
and has fascinating call graph support), I found the bottleneck.
It was Object#to_json.&lt;/p&gt;

&lt;p&gt;The original code is here:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;define_encoder String do |string|
  returning value = '"' do
    string.each_char do |char|
      value &amp;lt;&amp;lt; case
      when char == "\010":  '\b'
      when char == "\f":    '\f'
      when char == "\n":    '\n'
      when char == "\r":    '\r'
      when char == "\t":    '\t'
      when char == '"':     '\"'
      when char == '\':    '\\'
      when char.length &amp;gt; 1: "\u#{'%04x' % char.unpack('U').first}"
      else;                 char
      end
    end
    value &amp;lt;&amp;lt; '"'
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ruby can't handle characters fast, so this code is very slow.
You'd should use builtin methods such as String#gsub to handle multiple characters at once.&lt;/p&gt;

&lt;p&gt;I put the following code into config/environment.rb, and it looks quite fast:)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class String
  JSON_ESCAPED = {
     "\010" =&amp;gt;  '\b',
     "\f" =&amp;gt;    '\f',
     "\n" =&amp;gt;    '\n',
     "\r" =&amp;gt;    '\r',
     "\t" =&amp;gt;    '\t',
     '"' =&amp;gt;     '\"',
     '\' =&amp;gt;    '\\'
  }

  def to_json
    return '"' + gsub(/[\010\f\n\r\t"\]/) { |s|
      JSON_ESCAPED[s]
    } + '"'
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then I posted a patch to &lt;a href="http://dev.rubyonrails.org/ticket/3473"&gt;a related ticket&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If there is any reason to encode multibyte characters, more hacks are needed.
I think the best way is to encode multiple multibyte characters at once
using String#gsub and String#unpack, Array#pack.&lt;/p&gt;</description>
      <pubDate>Tue, 11 Jul 2006 12:51:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/07/11/to_json-too-slow</guid>
      <link>http://blog.shugo.net/articles/2006/07/11/to_json-too-slow</link>
      <category>Rails</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/20</trackback:ping>
    </item>
    <item>
      <title>Apache::RailsDispatcher supports Rails 1.1</title>
      <description>&lt;p&gt;Apache::RailsDispatcher in &lt;a href="https://projects.netlab.jp/svn/mod_ruby/mod_ruby/trunk"&gt;the SVN trunk&lt;/a&gt; of mod_ruby supports Rails 1.1 now:)
I will release mod_ruby-1.2.6 soon.&lt;/p&gt;

&lt;p&gt;But I can't run typo on it yet:(&lt;/p&gt;</description>
      <pubDate>Wed, 24 May 2006 22:31:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/05/24/apache-railsdispatcher-supports-rails-1-1</guid>
      <link>http://blog.shugo.net/articles/2006/05/24/apache-railsdispatcher-supports-rails-1-1</link>
      <category>mod_ruby</category>
      <category>Rails</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/19</trackback:ping>
    </item>
    <item>
      <title>RubyKaigi is Sold Out!</title>
      <description>&lt;p&gt;Tickets of &lt;a href="http://www.ruby-forum.com/topic/64343"&gt;RubyKaigi&lt;/a&gt; are sold out in a few hours!&lt;/p&gt;

&lt;p&gt;I'll talk about meta programming features of Ruby. See you in Tokyo.&lt;/p&gt;</description>
      <pubDate>Thu, 11 May 2006 06:35:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/05/11/rubykaigi-is-sold-out</guid>
      <link>http://blog.shugo.net/articles/2006/05/11/rubykaigi-is-sold-out</link>
      <category>Ruby</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/18</trackback:ping>
    </item>
    <item>
      <title>PessmisticLocking</title>
      <description>&lt;p&gt;&lt;a href="http://projects.netlab.jp/svn/rails_plugins/pessimistic_locking"&gt;PessimisticLocking&lt;/a&gt; provides row-level pessimistic locking using SELECT FOR UPDATE.&lt;/p&gt;

&lt;p&gt;You can specify the new option :lock of ActiveRecord::Base.find() to lock rows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Account.transaction do
  shugo = Account.find(:first, :conditions =&amp;gt; "name = 'shugo'", :lock =&amp;gt; true)
  yuko = Account.find(:first, :conditions =&amp;gt; "name = 'yuko'", :lock =&amp;gt; true)
  shugo.balance -= 100
  shugo.save
  yuko.balance += 100
  yuko.save
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you can also use ActiveRecord::Base#lock() instead:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Account.transaction do
  accounts = Account.find(:all, :conditions =&amp;gt; ...)
  account1 = accounts.detect { |account| ... }
  account2 = accounts.detect { |account| ... }
  account1.lock
  account2.lock
  account1.balance -= 100
  account1.save
  account2.balance += 100
  account2.save
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The latter way may be better if you don't need lock all SELECTed rows.&lt;/p&gt;</description>
      <pubDate>Thu, 11 May 2006 06:35:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/05/11/pessmisticlocking</guid>
      <link>http://blog.shugo.net/articles/2006/05/11/pessmisticlocking</link>
      <category>Rails</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/17</trackback:ping>
    </item>
    <item>
      <title>Japanese translation of AWDwR</title>
      <description>&lt;p&gt;The Japanese translation of &lt;a href="http://www.pragmaticprogrammer.com/titles/rails/index.html"&gt;Agile Web Development with Rails&lt;/a&gt;
will be available on Sat Feb 25:)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=4-274-06640-1"&gt;Official Site by Ohmsha&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://awdwr.shugo.net/"&gt;Support Site&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src="http://blog.shugo.net/photos/awdwr-ja.jpg" alt="Japanese translation of AWDwR"/&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 21 Feb 2006 15:23:00 JST</pubDate>
      <guid>http://blog.shugo.net/articles/2006/02/21/japanese-translation-of-awdwr</guid>
      <link>http://blog.shugo.net/articles/2006/02/21/japanese-translation-of-awdwr</link>
      <category>Rails</category>
      <trackback:ping>http://blog.shugo.net/articles/trackback/15</trackback:ping>
    </item>
  </channel>
</rss>
