<?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/" 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/</link>
		<comments>http://www.cslog.cn/Content/g-f-w-ni-ma-de/#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/">继续阅读 <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/">http://www.cslog.cn/Content/g-f-w-ni-ma-de/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/g-f-w-ni-ma-de/feed/</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/</link>
		<comments>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/#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/">继续阅读 <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/">http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/feed/</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/</link>
		<comments>http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/#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/">继续阅读 <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/">http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/BUG-cross-thread-violation-on-rb_gc-ruby-leopard/feed/</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/</link>
		<comments>http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/#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/">继续阅读 <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/">http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/howto-view-ruby-gem-rdoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby中10进制和16进制的转换</title>
		<link>http://www.cslog.cn/Content/ruby-hex-dec/</link>
		<comments>http://www.cslog.cn/Content/ruby-hex-dec/#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/">继续阅读 <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/">http://www.cslog.cn/Content/ruby-hex-dec/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby-hex-dec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby中如何复制对象 (deep clone)</title>
		<link>http://www.cslog.cn/Content/ruby-deep-clone/</link>
		<comments>http://www.cslog.cn/Content/ruby-deep-clone/#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/">继续阅读 <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/">http://www.cslog.cn/Content/ruby-deep-clone/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby-deep-clone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用Benchmark测试不同ruby代码的运行速度</title>
		<link>http://www.cslog.cn/Content/ruby_benchmark/</link>
		<comments>http://www.cslog.cn/Content/ruby_benchmark/#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/">继续阅读 <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/">http://www.cslog.cn/Content/ruby_benchmark/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ruby_benchmark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用ruby语言以句子为单位拆分一个字串</title>
		<link>http://www.cslog.cn/Content/string_to_sentences_with_ruby/</link>
		<comments>http://www.cslog.cn/Content/string_to_sentences_with_ruby/#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/">继续阅读 <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/">http://www.cslog.cn/Content/string_to_sentences_with_ruby/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/string_to_sentences_with_ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails string字符串相关转换</title>
		<link>http://www.cslog.cn/Content/rails_string_to_anything/</link>
		<comments>http://www.cslog.cn/Content/rails_string_to_anything/#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/">继续阅读 <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/">http://www.cslog.cn/Content/rails_string_to_anything/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/rails_string_to_anything/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>半semantic, 数据的价值和数据显示</title>
		<link>http://www.cslog.cn/Content/half_semantic/</link>
		<comments>http://www.cslog.cn/Content/half_semantic/#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/">继续阅读 <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/">http://www.cslog.cn/Content/half_semantic/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/half_semantic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
