<?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>船長日誌 &#187; Ruby on Rails</title>
	<atom:link href="http://www.cslog.cn/category/ruby-on-rails/feed/zh-hant/" rel="self" type="application/rss+xml" />
	<link>http://www.cslog.cn</link>
	<description>最讓我激動的是不知道下一個星球上能發現什麼...</description>
	<lastBuildDate>Wed, 30 Jul 2025 16:06:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>太噁心了&#8211; rubygems安裝gem需要使用代理服務器了</title>
		<link>http://www.cslog.cn/Content/g-f-w-ni-ma-de/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/g-f-w-ni-ma-de/zh-hant/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 07:43:14 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[privoxy]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rubygems]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=887</guid>
		<description><![CDATA[換了一台電腦，想用rubygems重新安裝rails，但一直遇到各種如問題： s &#8230; <a href="http://www.cslog.cn/Content/g-f-w-ni-ma-de/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>換了一台電腦，想用rubygems重新安裝rails，但一直遇到各種如問題：</p>
<pre>sudo gem install -v=2.3.9 rails
Password:
ERROR:  Could not find a valid gem 'rails' (= 2.3.9) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    too many connection resets (http://production.s3.rubygems.org/latest_specs.4.8.gz)</pre>
<p>或</p>
<pre>ERROR: While executing gem ... (Gem::DependencyError)
  Unable to resolve dependencies: rails requires activesupport (= 3.0.5), actionpack (= 3.0.5), activerecord (= 3.0.5), activeresource (= 3.0.5), actionmailer (= 3.0.5), railties (= 3.0.5), bundler (~&gt; 1.0)</pre>
<p>google了半天（期間google中短NNN次），才明天了現在<a href="http://hi.baidu.com/sitoto/blog/item/d00fd658fa6f469c810a181e.html">使用rubygems 安裝gem也得開代理了</a>。。。</p>
<p>要使用加了代理的命令：</p>
<pre>sudo gem install rails -p http://127.0.0.1:8118</pre>
<p>現在rubygems好像只支持http代理。 如果你使用的是socks代理，可以通過Privoxy（支持linux和mac)這個軟件來搞一個http代理 通道， 詳細參看<a href="http://www.cnblogs.com/another-wheel/archive/2011/11/16/setup-http-proxy-via-socks-and-privoxy.html">這個教程</a>。
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/g-f-w-ni-ma-de/zh-hant/">http://www.cslog.cn/Content/g-f-w-ni-ma-de/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/g-f-w-ni-ma-de/feed/zh-hant/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用Unicorn替代Mongrel作為Ruby on Rails的服務器</title>
		<link>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/zh-hant/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 16:54:41 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[站長文檔]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[unicorn]]></category>
		<category><![CDATA[服務器]]></category>
		<category><![CDATA[網站開發]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=727</guid>
		<description><![CDATA[先前的開發的Ruby on Rails網站使用的服務程序是Mongrel + N &#8230; <a href="http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>先前的開發的Ruby on Rails網站使用的服務程序是<a title="在ubuntu6.06上安裝apache2.2X + mysql + PHP + Ruby on Rails + Mongrel Cluster | 船長日誌" href="http://www.cslog.cn/Content/ubuntu606_mysql_apache2_ruby_rails_mongrel_cluster/">Mongrel + Nginx</a>， 現在用了Rails 3， 發現Mongrel對它有兼容問題， 所以要換一個。 雖然現在<a title="Overview — Phusion Passenger™ (a.k.a. mod_rails / mod_rack)" href="http://www.modrails.com/">Phusion Passenger</a>大行其道， 但使用前要重新編譯Nginx。 為了避免重新安裝nginx，我找到mongrel的替代品<a title="File: README [Unicorn: Rack HTTP server for fast clients and Unix]" href="http://unicorn.bogomips.org/">Unicorn</a>。 沒想到unicorn的設置非常方便。 我記錄在這裡供大家參考：</p>
<p>安裝unicorn:<br />
<code><br />
sudo gem install unicorn<br />
</code></p>
<p>創建網站配置文件(myproject是項目名稱):<br />
<code><span id="more-727"></span><br />
sudo mkdir /etc/unicorn<br />
cd /etc/unicorn/<br />
sudo nano /etc/unicorn/myproject.conf<br />
</code></p>
<p>內容如下：<br />
<code><br />
RAILS_ROOT=/www/myproject<br />
RAILS_ENV=production<br />
</code></p>
<p>在網站里再創建一個unicorn配置文件<br />
<code><br />
nano /www/myproject/config/unicorn.rb<br />
</code></p>
<p>內容如下：<br />
<code><br />
# Minimal sample configuration file for Unicorn (not Rack) when used<br />
# with daemonization (unicorn -D) started in your working directory.<br />
#<br />
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete<br />
# documentation.<br />
# See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for<br />
# a more verbose configuration using more features.</code></p>
<p><code> </code></p>
<p><code>app_path = "/www/myproject"</code></p>
<p><code> </code></p>
<p><code>listen 8080 # by default Unicorn listens on port 8080<br />
worker_processes 2 # this should be &gt;= nr_cpus<br />
pid "#{app_path}/tmp/pids/unicorn.pid"<br />
stderr_path "#{app_path}/log/unicorn.log"<br />
stdout_path "#{app_path}/log/unicorn.log"<br />
</code></p>
<p>設置unicorn啟動腳本：<br />
<code><br />
sudo nano /etc/init.d/unicorn_init<br />
</code><br />
腳本內容：<br />
<code><br />
#!/bin/sh<br />
#<br />
# init.d script for single or multiple unicorn installations. Expects at least one .conf<br />
# file in /etc/unicorn<br />
#<br />
# Modified by jay@gooby.org http://github.com/jaygooby<br />
# based on http://gist.github.com/308216 by http://github.com/mguterl<br />
#<br />
## A sample /etc/unicorn/my_app.conf<br />
##<br />
## RAILS_ENV=production<br />
## RAILS_ROOT=/var/apps/www/my_app/current<br />
#<br />
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in<br />
# production mode. It will read config/unicorn.rb for further set up.<br />
#<br />
# You should ensure different ports or sockets are set in each config/unicorn.rb if<br />
# you are running more than one master concurrently.<br />
#<br />
# If you call this script without any config parameters, it will attempt to run the<br />
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf<br />
#<br />
# /etc/init.d/unicorn start # starts all unicorns<br />
#<br />
# If you specify a particular config, it will only operate on that one<br />
#<br />
# /etc/init.d/unicorn start /etc/unicorn/my_app.conf</code></p>
<p><code> </code></p>
<p><code>set -e</code></p>
<p><code>sig () {<br />
test -s "$PID" &amp;&amp; kill -$1 `cat "$PID"`<br />
}</p>
<p>oldsig () {<br />
test -s "$OLD_PID" &amp;&amp; kill -$1 `cat "$OLD_PID"`<br />
}</p>
<p>cmd () {</p>
<p>case $1 in<br />
start)<br />
sig 0 &amp;&amp; echo &gt;&amp;2 "Already running" &amp;&amp; exit 0<br />
echo "Starting"<br />
$CMD<br />
;;<br />
stop)<br />
sig QUIT &amp;&amp; echo "Stopping" &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Not running"<br />
;;<br />
force-stop)<br />
sig TERM &amp;&amp; echo "Forcing a stop" &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Not running"<br />
;;<br />
restart|reload)<br />
sig USR2 &amp;&amp; sleep 5 &amp;&amp; oldsig QUIT &amp;&amp; echo "Killing old master" `cat $OLD_PID` &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't reload, starting '$CMD' instead"<br />
$CMD<br />
;;<br />
upgrade)<br />
sig USR2 &amp;&amp; echo Upgraded &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't upgrade, starting '$CMD' instead"<br />
$CMD<br />
;;<br />
rotate)<br />
sig USR1 &amp;&amp; echo rotated logs OK &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't rotate logs" &amp;&amp; exit 1<br />
;;<br />
*)<br />
echo &gt;&amp;2 "Usage: $0 "<br />
exit 1<br />
;;<br />
esac<br />
}</p>
<p>setup () {</p>
<p>echo -n "$RAILS_ROOT: "<br />
cd $RAILS_ROOT || exit 1<br />
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid<br />
export OLD_PID="$PID.oldbin"</p>
<p>CMD="/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D"<br />
}</p>
<p>start_stop () {</p>
<p># either run the start/stop/reload/etc command for every config under /etc/unicorn<br />
# or just do it for a specific one</p>
<p># $1 contains the start/stop/etc command<br />
# $2 if it exists, should be the specific config we want to act on<br />
if [ $2 ]; then<br />
. $2<br />
setup<br />
cmd $1<br />
else<br />
for CONFIG in /etc/unicorn/*.conf; do<br />
# import the variables<br />
. $CONFIG<br />
setup</p>
<p># run the start/stop/etc command<br />
cmd $1<br />
done<br />
fi<br />
}</p>
<p></code></p>
<p><code>ARGS="$1 $2"<br />
start_stop $ARGS<br />
</code><br />
注意將裡面的/usr/bin/unicorn_rails 換成你系統中unicorn_rails程序的實際路徑。</p>
<p>設置unicorn_init文件屬性:<br />
<code><br />
sudo chmod 755 /etc/init.d/unicorn_init<br />
</code></p>
<p>啟動unicorn:<br />
<code><br />
/etc/init.d/unicorn_init<br />
</code></p>
<p>修改nginx的配置文件，加入unicorn的代理設置：<br />
<code><br />
upstream myproject_mongrel {<br />
server 127.0.0.1:8080 fail_timeout=0;<br />
}<br />
</code><br />
這部分跟使用mongrel的類似的。</p>
<p>這樣unicorn的設置就完成了。 剛設置好，感覺unicorn跟mongrel一樣， 都是比較吃內存的， 一啟動就佔了50M. 不知道會不會也像mongrel一樣把內存吃爆， 會得話得設置監控軟件（如god）看住它。
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/zh-hant/">http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/feed/zh-hant/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[BUG] cross-thread violation on rb_gc()</title>
		<link>http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/zh-hant/#comments</comments>
		<pubDate>Wed, 13 May 2009 18:08:11 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[今天在mac os x leopard上安裝typo時遇到 [BUG] cros &#8230; <a href="http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>今天在mac os x leopard上安裝typo時遇到<br />
[BUG] cross-thread violation on rb_gc()<br />
錯誤.<br />
google了一下, 發下有可能是因為系統上安裝了多個版本的ruby 引起的. 我的mac上的確有多個ruby: 一個是leopard原帶的, 另一個是自己用macport安裝的. 但是系統原帶的ruby1.8.6已經在清理硬盤空間時給刪除了, 文件快捷鏈接也都換了, 使用ruby -v也顯示是1.8.7版的信息. <br />
全系統搜索了一下, 竟然發現系統上還有個<br />
/System/Library/Frameworks/Ruby.framework/Versions/Current/usr<br />
裡面帶有ruby文件.<br />
將它去除, 改成一個文件快捷鏈接到自己安裝的那個ruby文件那裡, 問題解決了.
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/zh-hant/">http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/feed/zh-hant/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>怎樣看本地ruby gem的rDoc文檔</title>
		<link>http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/zh-hant/#comments</comments>
		<pubDate>Mon, 11 May 2009 17:58:31 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[rdoc]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[在本地查看rDoc文檔其實很簡單, 只要運行: gem server 你就可以用 &#8230; <a href="http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>在本地查看rDoc文檔其實很簡單, 只要運行:<br />
gem server<br />
你就可以用瀏覽器通過 http://localhost:8808/ 網址查看所有已經安裝的ruby gem的rDoc文檔了.
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/zh-hant/">http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby中10進制和16進制的轉換</title>
		<link>http://www.cslog.cn/Content/ruby-hex-dec/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/ruby-hex-dec/zh-hant/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 23:27:08 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[16進制到10進制: &#62;&#62; &#34;B0A0&#34;.hex &#8230; <a href="http://www.cslog.cn/Content/ruby-hex-dec/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>16進制到10進制:<br />
&gt;&gt; &quot;B0A0&quot;.hex<br />
=&gt; 45216<br />
或<br />
&gt;&gt; &quot;B0A0&quot;.to_i(16)<br />
=&gt; 45216<br />
10進制到16進制:<br />
&gt;&gt; 45216.to_s(16)<br />
=&gt; &quot;b0a0&quot;
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/ruby-hex-dec/zh-hant/">http://www.cslog.cn/Content/ruby-hex-dec/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby-hex-dec/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby中如何複製對象 (deep clone)</title>
		<link>http://www.cslog.cn/Content/ruby-deep-clone/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/ruby-deep-clone/zh-hant/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 21:47:20 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[用Ruby複製一個對象(object)也許沒有你想像的那麼容易. 今天我goog &#8230; <a href="http://www.cslog.cn/Content/ruby-deep-clone/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>用Ruby複製一個對象(object)也許沒有你想像的那麼容易. 今天我google了半天, 做個總結吧.<br />
先從最簡單的開始, b = a 是複製嗎? 看代碼說話:<br />
&gt;&gt; a= [0,[1,2]]<br />
&gt;&gt; b=a<br />
&gt;&gt; b[0]=88<br />
&gt;&gt; b[1][0]=99<br />
&gt;&gt; b&nbsp; <br />
=&gt; [88, [99, 2]]<br />
&gt;&gt; a&nbsp; <br />
=&gt; [88, [99, 2]]<br />
從上面代碼發現, 一但修改b, 原來的a也同時被改變了. 甚至:</p>
<p>&gt;&gt; b.equal?(a)<br />
=&gt; true<br />
原來b跟a根本就是同一個object, 只是馬甲不一樣罷了. 所以b = a不是複製.<br />
那 b = a.dup呢?? 還是看代碼:<br />
&gt;&gt; a= [0,[1,2]]<br />
&gt;&gt; b=a.dup<br />
&gt;&gt; b[0]=88<br />
&gt;&gt; b[1][0]=99<br />
&gt;&gt; b<br />
=&gt; [88, [99, 2]]<br />
&gt;&gt; a<br />
=&gt; [0, [99, 2]]<br />
情況似乎有所好轉, 在修改b後, a還是有一部分被修改了.(0沒有變,但原來的1變成了99).<br />
所以dup有時候是複製(如在Array只有一級時), 但有時不是複製哦.<br />
再來一個, b = a.clone呢? 上代碼:<br />
&gt;&gt; a= [0,[1,2]]<br />
&gt;&gt; b=a.clone<br />
&gt;&gt; b[0]=88<br />
&gt;&gt; b[1][0]=99<br />
&gt;&gt; b<br />
=&gt; [88, [99, 2]]<br />
&gt;&gt; a<br />
=&gt; [0, [99, 2]]<br />
情況幾乎跟dup一模一樣. 所以clone也不一定可以相信哦! <br />
原來ruby中的dup和clone都是shallow複製, 只針對object的第一級屬性. <br />
汗, 難道在Ruby中沒有辦法複製對像嗎? 也不完全是, 看這個:<br />
&gt;&gt; a= [0,[1,2]]<br />
&gt;&gt; b=Marshal.load(Marshal.dump(a))<br />
&gt;&gt; b[0]=88<br />
&gt;&gt; b[1][0]=99<br />
&gt;&gt; b<br />
=&gt; [88, [99, 2]]<br />
&gt;&gt; a= [0,[1,2]]<br />
=&gt; [0, [1, 2]]<br />
修改b後a沒有被改變!!! 似乎終於成功找到複製的辦法了!!!<br />
為什麼要加&quot;似乎&quot;呢? 因為有些object是不能被Marshal.dump的.如:<br />
&gt;&gt; t=Object.new<br />
&gt;&gt; def t.test; puts &#8216;test&#8217; end<br />
&gt;&gt; Marshal.dump(t)<br />
TypeError: singleton can&#8217;t be dumped<br />
&nbsp;&nbsp;&nbsp; from (irb):59:in `dump&#8217;<br />
&nbsp;&nbsp;&nbsp; from (irb):59<br />
更完善的複製方案可以考慮給ruby增加一個deep clone功能, 可以參考以下鏈接:<br />
<a href="http://d.hatena.ne.jp/pegacorn/20070417/1176817721">http://d.hatena.ne.jp/pegacorn/20070417/1176817721</a><br />
<a href="http://www.artima.com/forums/flat.jsp?forum=123&amp;thread=40913">http://www.artima.com/forums/flat.jsp?forum=123&amp;thread=40913</a><br />
好了, 複製這個小問題, 沒想到也能引出這麼長的文章來, 沒想到吧?
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/ruby-deep-clone/zh-hant/">http://www.cslog.cn/Content/ruby-deep-clone/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby-deep-clone/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用Benchmark測試不同ruby代碼的運行速度</title>
		<link>http://www.cslog.cn/Content/ruby_benchmark/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/ruby_benchmark/zh-hant/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 23:22:27 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[如果比較功能相同的幾組代碼的運行速度, 可以使用ruby語言里的Benchmar &#8230; <a href="http://www.cslog.cn/Content/ruby_benchmark/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>如果比較功能相同的幾組代碼的運行速度, 可以使用ruby語言里的<a href="http://ruby-doc.org/core/classes/Benchmark.html">Benchmark</a>模塊.<br />
(開始前 設置中文環境, $KCODE = &#8216;u&#8217;)<br />
比如, 連接兩個字串, 如:<br />
str1 = &quot;中國&quot;<br />
str2 = &quot;一定強!!!&quot;</p>
<p>可以使用+<br />
?&gt; str1 + str2<br />
=&gt; &quot;中國一定強!!!&quot;<br />
也可使用<br />
&gt;&gt; &quot;#{str1}#{str2}&quot;<br />
=&gt; &quot;中國一定強!!!&quot;<br />
還可以使用&lt;&lt;<br />
&gt;&gt; str1 &lt;&lt; str2<br />
=&gt; &quot;中國一定強!!!&quot;<br />
(注意這種方法會改變str1的值, <br />
&gt;&gt; str1<br />
=&gt; &quot;中國一定強!!!&quot;)</p>
<p>如果想要知道這三種方法哪一個速度更快, 就可以使用Benchmark比較.<br />
先定義三個不同的方法:</p>
<p>
def joined_by_plus(str_a, str_b)<br />
&nbsp;&nbsp;&nbsp; 500000.times do; str_a.dup + str_b; end<br />
end</p>
<p>def joined_by_uniting(str_a, str_b)<br />
&nbsp;&nbsp;&nbsp; 500000.times do; &quot;#{str_a.dup}#{str_b}&quot;; end<br />
end</p>
<p>def joined_by_adding(str_a, str_b)<br />
&nbsp;&nbsp;&nbsp; 500000.times do; str_a.dup &lt;&lt; str_b; end<br />
end</p>
<p>因為這三種方法速度都很快, 比較一次的時間很難分出上下, 所以每種方法都做500000次.<br />
因為&lt;&lt; 會改變變量, 所以在第三個方法中使用了str_a.dup, 備份出一個str_a來測試, 其它二個方法雖然從理論上不須要這個dup, 但為了時間上的公平, 也給它們加上了.</p>
<p>下面是測試代碼:<br />
require &#8216;benchmark&#8217;</p>
<p>Benchmark.bmbm(10) do |t|<br />
&nbsp; t.report(&#8216;加號連接&#8217;) { joined_by_plus(str1, str2) }<br />
&nbsp; t.report(&#8216;放進引號&#8217;) { joined_by_uniting(str1, str2)}<br />
&nbsp; t.report(&#8216;使用&lt;&lt;&#8217;) { joined_by_adding(str1, str2)}<br />
end</p>
<p>bmbm是Benchmark的測試方法, 這是雙重測試, 用來提高精確度. 裡面參數10用來調節標籤在結果里的顯示佔位寬度, 不對結果產生影響.<br />
report(&#8216;加號連接&#8217;)里的參數是為相應測試的標籤名, 用來區別測試結果.</p>
<p>下面就是結果:</p>
<p>Rehearsal &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
加號連接&nbsp;&nbsp; 1.800000&nbsp;&nbsp; 0.580000&nbsp;&nbsp; 2.380000 (&nbsp; 2.435614)<br />
放進引號&nbsp;&nbsp; 2.080000&nbsp;&nbsp; 0.590000&nbsp;&nbsp; 2.670000 (&nbsp; 2.706669)<br />
使用&lt;&lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.000000&nbsp;&nbsp; 0.580000&nbsp;&nbsp; 2.580000 (&nbsp; 2.635540)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; total: 7.630000sec</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user&nbsp;&nbsp;&nbsp;&nbsp; system&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; real<br />
加號連接&nbsp;&nbsp; 1.810000&nbsp;&nbsp; 0.600000&nbsp;&nbsp; 2.410000 (&nbsp; 2.476613)<br />
放進引號&nbsp;&nbsp; 2.050000&nbsp;&nbsp; 0.600000&nbsp;&nbsp; 2.650000 (&nbsp; 2.656080)<br />
使用&lt;&lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.960000&nbsp;&nbsp; 0.600000&nbsp;&nbsp; 2.560000 (&nbsp; 2.567434)</p>
<p>因為使用了bmbm方式, 所以會有兩個結果, 第一個是演習結果, 第二個是真實結果.&nbsp; 如果使用bm方法, 將只會有一組結果.<br />
結果有user時間, 系統時間, 總時間和實際花費的時間. 單位為秒.</p>
<p>從結果可以看出, 使用+連接兩個字串時速度最快, 而放進引號的方法最慢. 不過從結果也可以看出即使進行500000執行的結果也相差在0.3秒之內, 平時換用問題不大.</p>
<p>看來不是這一次, 希望下次你在自己的Benchmark試驗中, 會發現給你的程序帶來巨大速度提供的代碼.
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/ruby_benchmark/zh-hant/">http://www.cslog.cn/Content/ruby_benchmark/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby_benchmark/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用ruby語言以句子為單位拆分一個字串</title>
		<link>http://www.cslog.cn/Content/string_to_sentences_with_ruby/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/string_to_sentences_with_ruby/zh-hant/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 20:42:05 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[目標, 用ruby語言以句子為單位拆分一個字串. 如: 給出字串: s=&#038;quo &#8230; <a href="http://www.cslog.cn/Content/string_to_sentences_with_ruby/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>目標, 用ruby語言以句子為單位拆分一個字串. 如:<br />
給出字串:<br />
s=&quot;這是一個句子,另一個句子.一個新句子!難道這不是另一個句子嗎?&quot;<br />
將它分拆為:<br />
&nbsp;[&quot;這是一個句子,&quot;], [&quot;另一個句子.&quot;], [&quot;一個新句子!&quot;], [&quot;難道這不是另一個句子嗎?&quot;]<br />
樣式.<br />
測試前將ruby的$KCODE環境變量設置為&#8217;u', 啟動UTF8編碼支持中文.<br />
$KCODE=&#8217;u&#8217;<br />
嘗試1:<br />
&gt;&gt; s.split(/\.|,|\?|\!/)<br />
=&gt; [&quot;這是一個句子&quot;, &quot;另一個句子&quot;, &quot;一個新句子&quot;, &quot;難道這不是另一個句子嗎&quot;]<br />
等同於:<br />
&gt;&gt; s.split(/[.,!?]/)<br />
=&gt; [&quot;這是一個句子&quot;, &quot;另一個句子&quot;, &quot;一個新句子&quot;, &quot;難道這不是另一個句子嗎&quot;]<br />
雖然分開了句子, 但失去了標點.</p>
<p>&gt;&gt; s.split(/([.,!?])/)<br />
=&gt; [&quot;這是一個句子&quot;, &quot;,&quot;, &quot;另一個句子&quot;, &quot;.&quot;, &quot;一個新句子&quot;, &quot;!&quot;, &quot;難道這不是另一個句子嗎&quot;, &quot;?&quot;]<br />
用括號將正則組成一組, 標點出來了, 但和句子是分開的.</p>
<p>改用scan<br />
&gt;&gt; s.scan /.+?[.,?!]/<br />
=&gt; [&quot;這是一個句子,&quot;], [&quot;另一個句子.&quot;], [&quot;一個新句子!&quot;], [&quot;難道這不是另一個句子嗎?&quot;]<br />
成功了!!</p>
<p>如果一定要這樣:<br />
&gt;&gt; s.scan(/(.+?(\.|\?|\!))/).collect{|p|p[0]}<br />
=&gt; [&quot;這是一個句子,另一個句子.&quot;, &quot;一個新句子!&quot;, &quot;難道這不是另一個句子嗎?&quot;]<br />
也是可行的.
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/string_to_sentences_with_ruby/zh-hant/">http://www.cslog.cn/Content/string_to_sentences_with_ruby/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/string_to_sentences_with_ruby/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails string字符串相關轉換</title>
		<link>http://www.cslog.cn/Content/rails_string_to_anything/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/rails_string_to_anything/zh-hant/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 23:43:00 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[總結字串轉變到其它與它相關的object的方法. 在這裡整理分享一下, 希望能幫 &#8230; <a href="http://www.cslog.cn/Content/rails_string_to_anything/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>總結字串轉變到其它與它相關的object的方法. 在這裡整理分享一下, 希望能幫助到你.</p>
<p>變量相關<br />
讀取</p>
<pre><span class="ident">instance_variable_get</span><span class="punct">(&quot;</span><span class="string">@name</span><span class="punct">&quot;)</span>     <span class="comment">#返回@name值</span></pre>
<p>設置</p>
<pre><span class="ident">instance_variable_set</span><span class="punct">(&quot;</span><span class="string">@name</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">三國演義</span><span class="punct">&quot;)</span> <span class="comment">#@name =  &quot;三國演義&quot;</span></pre>
<p>method相關<br />
string到method名<br />
send</p>
<pre><span class="ident">book</span><span class="punct">.</span><span class="ident">send</span><span class="punct">(&quot;</span><span class="string">name</span><span class="punct">&quot;)</span> <span class="comment">#返回 book.name值</span></pre>
<p>注意: book.send(&quot;name&quot;) = &quot;三國演義&quot; 會出錯. <br />
但可以:</p>
<pre><span class="ident">book</span><span class="punct">.</span><span class="ident">send</span><span class="punct">(&quot;</span><span class="string">name=</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">三國演義</span><span class="punct">&quot;)</span> <span class="comment">#book.name = &quot;三國演義&quot;</span></pre>
<p>
單複數轉化<br />
string複數化<br />
pluralize<br />
可用於生成table_name</p>
<pre><span class="punct">&quot;</span><span class="string">country</span><span class="punct">&quot;.</span><span class="ident">pluralize</span> <span class="comment">#=&gt; &quot;countries&quot;</span></pre>
<p>
string單數化<br />
singularize<br />
和上面的相反</p>
<pre><span class="punct">&quot;</span><span class="string">posts</span><span class="punct">&quot;.</span><span class="ident">singularize</span> <span class="comment">#=&gt; &quot;post&quot;</span></pre>
<p>
table和class相關</p>
<p>將表格名轉成class名<br />
classify</p>
<pre><span class="punct">&quot;</span><span class="string">blog_pictures</span><span class="punct">&quot;.</span><span class="ident">classify</span> <span class="comment">#=&gt; &quot;BlogPicture&quot;</span></pre>
<p>
轉成table_name(和上面的相反)<br />
tableize</p>
<pre><span class="punct">&quot;</span><span class="string">BlogPicture</span><span class="punct">&quot;.</span><span class="ident">tableize</span> <span class="comment">#=&gt; &quot;blog_pictures&quot;</span></pre>
<p>
將class名轉成class<br />
constantize</p>
<pre><span class="punct">&quot;</span><span class="string">Country</span><span class="punct">&quot;.</span><span class="ident">constantize</span> <span class="comment">#=&gt; Country</span></pre>
<p>
可以將classify和constantize聯用將string轉成class model</p>
<pre><span class="punct">&quot;</span><span class="string">books</span><span class="punct">&quot;.</span><span class="ident">classify</span><span class="punct">.</span><span class="ident">constantize</span><span class="punct">.</span><span class="ident">find_by_name</span><span class="punct">(&quot;</span><span class="string">三國演義</span><span class="punct">&quot;)</span><br /><span class="comment">#=&gt; Book.find_by_name(&quot;三國演義&quot;)</span></pre>
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/rails_string_to_anything/zh-hant/">http://www.cslog.cn/Content/rails_string_to_anything/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/rails_string_to_anything/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>半semantic, 數據的價值和數據顯示</title>
		<link>http://www.cslog.cn/Content/half_semantic/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/half_semantic/zh-hant/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 21:40:57 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[信息處理]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[讓機器讀懂之前先讓人讀懂! 出發點? &#160;數據的價值不一, 因此不應該用 &#8230; <a href="http://www.cslog.cn/Content/half_semantic/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>讓機器讀懂之前先讓人讀懂!<br />
出發點?<br />
&nbsp;數據的價值不一, 因此不應該用同樣的方式顯示這些數據.<br />
&nbsp;<br />
價值? <br />
價值針對不同個體應該是不一樣的, 有條件最好做到面向不同個體(根據IP地址, 性別, 年齡, 歷史記錄), 如果做不到就盡量力求滿足最大群體.</p>
<p>數據怎樣的價值?<br />
有用?/沒有用?<br />
重要?/不重要?<br />
要緊?/不要緊?</p>
<p>不同的顯示?<br />
文字:字體, 字體大小, 顏色, 底色&#8230; 直接改用圖標?<br />
位置,:左上角?&nbsp; 扔到最下面?</p>
<p>方案<br />
數據和顯示的對接<br />
數據輸出時附帶顯示模式信息, 對準備好的view pattern直接對接<br />
out =&gt; (data, view_options)<br />
@title, @content, @sidebar, @item.name, @item.content 變量名直接附帶意義</p>
<p>比rails更rails<br />
使用統一的column名, 如name, content&#8230;<br />
使用統一的變量名, 如@title, @item&#8230;<br />
使用統一的view patterns, 如用來顯示列表的index頁面, 換到哪個model一樣使用.<br />
使用統一的view 局部pattern(最簡單, google, wikipedia化, 實用, 可以指出數據的重要性, 美觀不是第一的, 美觀還費帶寬和美工), 如可以重複使用的list顯示pattern, comments顯示pattern&#8230;.
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/half_semantic/zh-hant/">http://www.cslog.cn/Content/half_semantic/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/half_semantic/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- WP Chinese Conversion Full Page Converted -->