<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Wancw's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 07 Aug 2008 19:56:46 GMT</pubDate>
    <description>DZone Snippets: Wancw's Code Snippets</description>
    <item>
      <title>Show Stars - Loop Version</title>
      <link>http://snippets.dzone.com/posts/show/5685</link>
      <description>&lt;br /&gt;&#38988;&#30446; &lt;a href="http://www.mobile01.com/topicdetail.php?f=300&amp;t=415016&amp;m=f&amp;r=4&amp;last=6753118#11"&gt;http://www.mobile01.com/topicdetail.php?f=300&amp;t=415016&amp;m=f&amp;r=4&amp;last=6753118#11&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int main (int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;        int N, total_line, spl, l, n;&lt;br /&gt;        scanf ("%d", &amp;N);&lt;br /&gt;        total_line = N; // &#31532;&#20108;&#38988;&#35201;&#25226; N &#25913;&#25104; (N &lt;&lt; 1) - 1;&lt;br /&gt;&lt;br /&gt;        for (spl = 1, l = 0, n = 0; l &lt; total_line; )&lt;br /&gt;        {&lt;br /&gt;                putchar ('*');&lt;br /&gt;                ++n;&lt;br /&gt;                if (n&gt;=spl)&lt;br /&gt;                {&lt;br /&gt;                        putchar ('\n');&lt;br /&gt;                        ++l;&lt;br /&gt;                        spl += (l&lt;N) ? 1 : -1;&lt;br /&gt;                        n = 0;&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt;        return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jun 2008 06:56:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5685</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>Show Stars - Recursive Version</title>
      <link>http://snippets.dzone.com/posts/show/5684</link>
      <description>&lt;br /&gt;&#38988;&#30446; &lt;a href="http://www.mobile01.com/topicdetail.php?f=300&amp;t=415016&amp;m=f&amp;r=4&amp;last=6753118#11"&gt;http://www.mobile01.com/topicdetail.php?f=300&amp;t=415016&amp;m=f&amp;r=4&amp;last=6753118#11&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;void show_star (n)&lt;br /&gt;{&lt;br /&gt;        if (n == 0)&lt;br /&gt;                putchar('\n');&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;                putchar ('*');&lt;br /&gt;                show_star(n-1);&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void show_line (int from, int to, int step)&lt;br /&gt;{&lt;br /&gt;        show_star (from);&lt;br /&gt;        if (from != to)&lt;br /&gt;                show_line (from+step, to, step);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main (int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;        int N;&lt;br /&gt;        scanf ("%d", &amp;N);&lt;br /&gt;        show_line(1, N, 1);&lt;br /&gt;        /* &#21152;&#19978;&#19979;&#38754;&#36889;&#34892;&#23601;&#26159;&#31532;&#20108;&#38988; */&lt;br /&gt;        /* show_line(N-1, 1, -1); */&lt;br /&gt;        return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jun 2008 06:52:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5684</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>Include javascript by DOM</title>
      <link>http://snippets.dzone.com/posts/show/4373</link>
      <description>Orignial: http://www.codepost.org/browse/snippets/85&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function include(file)&lt;br /&gt;{&lt;br /&gt;      var script  = document.createElement('script');&lt;br /&gt;      script.src  = file;&lt;br /&gt;      script.type = 'text/javascript';&lt;br /&gt;&lt;br /&gt;      document.getElementsByTagName('head').item(0).appendChild(script);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 31 Jul 2007 13:55:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4373</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>Include javascript by XMLHttpRequest</title>
      <link>http://snippets.dzone.com/posts/show/4372</link>
      <description>&lt;br /&gt;Original from &lt;a href="http://www.exit12.org/archives/12"&gt;http://www.exit12.org/archives/12&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function include(jsFileLocation)&lt;br /&gt;{&lt;br /&gt;	if(window.XMLHttpRequest)&lt;br /&gt;	{&lt;br /&gt;		var req = new XMLHttpRequest();&lt;br /&gt;	}&lt;br /&gt;	else&lt;br /&gt;	{&lt;br /&gt;		var req = new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;	}&lt;br /&gt;	req.open("GET", jsFileLocation,false);&lt;br /&gt;	req.onreadystatechange = function()&lt;br /&gt;	{&lt;br /&gt;		if (req.readyState == 4)&lt;br /&gt;		{&lt;br /&gt;			window.eval(req.responseText);&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	req.send(null);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 31 Jul 2007 13:51:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4372</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>Multiple Rails Applications with lighttpd</title>
      <link>http://snippets.dzone.com/posts/show/3758</link>
      <description>&lt;code&gt;&lt;br /&gt;# lighttpd.conf&lt;br /&gt;$HTTP["url"] =~ "^/base_url_to_app1/" {&lt;br /&gt;  server.document-root = "/path/to/app1/public/"&lt;br /&gt;  alias.url = ( "/base_url_to_app1/" =&gt; "/path/to/app1/public/" )&lt;br /&gt;  server.error-handler-404 = "/base_url_to_app1/dispatch.fcgi"&lt;br /&gt;  fastcgi.server = (&lt;br /&gt;    ".fcgi" =&gt; ( (&lt;br /&gt;      "socket" =&gt; "/tmp/app1.socket",&lt;br /&gt;      "bin-path" =&gt;  "/path/to/app1/public/dispatch.fcgi",&lt;br /&gt;    ) )&lt;br /&gt;  )&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$HTTP["url"] =~ "^/base_url_to_app2/" {&lt;br /&gt;  # ...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# config/environment.rb&lt;br /&gt;module ActionController&lt;br /&gt;  class AbstractRequest&lt;br /&gt;    alias_method :orig_rel_url_root, :relative_url_root&lt;br /&gt;    def relative_url_root&lt;br /&gt;      if (@env['SCRIPT_NAME'] &amp;&amp; /\/dispatch\.(fcgi|rb|cgi)$/ =~ @env['SCRIPT_NAME'])&lt;br /&gt;        @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')&lt;br /&gt;      else&lt;br /&gt;        orig_rel_url_root&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 03 Apr 2007 03:32:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3758</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>General Delimited Input in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2978</link>
      <description>In ruby there are several special literal notations:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;%{blah}       # or %Q{blah} # same as "blah" but igornes " and '&lt;br /&gt;%q{blah}      # same as 'blah' but no interpolation&lt;br /&gt;%w{blah blah} # same as "blah blah".split&lt;br /&gt;%r{blah}      # same as /blah/&lt;br /&gt;%x{ls}        # same as `ls`&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 06 Nov 2006 22:52:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2978</guid>
      <author>WanCW (WanCW)</author>
    </item>
    <item>
      <title>Overload for Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2642</link>
      <description>An module let you overload functions&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Example:&lt;br /&gt;#&lt;br /&gt;# class Test1&lt;br /&gt;#   include Overload&lt;br /&gt;# &lt;br /&gt;#   def foo&lt;br /&gt;#     puts "foo # Original one"&lt;br /&gt;#   end&lt;br /&gt;# &lt;br /&gt;#   overload :foo, :foo_str, String do |str, x|&lt;br /&gt;#     puts "foo_str(String str, x)"&lt;br /&gt;#   end&lt;br /&gt;# &lt;br /&gt;#   overload :foo, Integer do |i|&lt;br /&gt;#     puts "foo(Integer i) # no specific name"&lt;br /&gt;#   end&lt;br /&gt;# end&lt;br /&gt;module Overload&lt;br /&gt;&lt;br /&gt;  def self.included(klass)&lt;br /&gt;    class &lt;&lt; klass&lt;br /&gt;      include Overload::Feature&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.spec_to_cond(spec)&lt;br /&gt;    code = "args.length == #{spec[0]}"&lt;br /&gt;    spec[1..-1].each_with_index { |s, i|&lt;br /&gt;      code &lt;&lt; " &amp;&amp; #{s.inspect} === args[#{i}]"&lt;br /&gt;    }&lt;br /&gt;    code&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  module Feature&lt;br /&gt;&lt;br /&gt;    private&lt;br /&gt;&lt;br /&gt;    def overload(method_id, *spec, &amp;block)&lt;br /&gt;      if spec[0].instance_of? Symbol&lt;br /&gt;        specific_method_id = spec.shift&lt;br /&gt;        define_method(specific_method_id, block) if block&lt;br /&gt;        block = instance_method(specific_method_id)&lt;br /&gt;      elsif block&lt;br /&gt;        define_method(method_id, block)&lt;br /&gt;        block = instance_method(method_id)&lt;br /&gt;        remove_method(method_id)&lt;br /&gt;      else&lt;br /&gt;        raise "You must give a block or a name of existing method!"&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      spec.unshift(block.arity)&lt;br /&gt;      add_method_to_dispatch_table(method_id, spec, block)&lt;br /&gt;&lt;br /&gt;      update_dispatcher(method_id)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def add_method_to_dispatch_table(method_id, key, block)&lt;br /&gt;      if !dispatch_table[method_id]&lt;br /&gt;        dispatch_table[method_id] = Hash.new&lt;br /&gt;      end&lt;br /&gt;      dispatch_table[method_id][key] = block&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def update_dispatcher(method_id)&lt;br /&gt;      remove_method(method_id) if method_defined?(method_id)&lt;br /&gt;      class_eval dispatcher_code(method_id), "#{method_id}_dispatcher", 0&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def dispatcher_code(method_id)&lt;br /&gt;      method_body = &lt;&lt;-EOS&lt;br /&gt;        def #{method_id.to_s}(*args, &amp;blk)&lt;br /&gt;          dt = DispatchTable[#{method_id.inspect}]&lt;br /&gt;      EOS&lt;br /&gt;&lt;br /&gt;      sorted_dispatch_table_of(method_id).each_with_index do |(spec, block), i|&lt;br /&gt;        method_body &lt;&lt; &lt;&lt;-EOS&lt;br /&gt;          #{(i==0) ? "if" : "elsif"} #{Overload::spec_to_cond(spec)}&lt;br /&gt;            dt[#{spec.inspect}].bind(self).call(*args, &amp;blk)&lt;br /&gt;        EOS&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      method_body &lt;&lt; &lt;&lt;-EOS&lt;br /&gt;          else&lt;br /&gt;            raise "Couldn't find method for #{method_id}(\#{args.inspect[1..-2]})"&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      EOS&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def sorted_dispatch_table_of(method_id)&lt;br /&gt;      dispatch_table[method_id].sort do |a,b|&lt;br /&gt;        a[0].length &lt;=&gt; b[0].length&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def dispatch_table&lt;br /&gt;      const_set("DispatchTable", Hash.new) if !const_defined?("DispatchTable")&lt;br /&gt;      const_get("DispatchTable")&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Sep 2006 13:49:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2642</guid>
      <author>WanCW (WanCW)</author>
    </item>
  </channel>
</rss>
