<?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>江淼的Blog &#187; ruby</title>
	<atom:link href="http://www.jiangmiao.org/blog/c/programme/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jiangmiao.org/blog</link>
	<description>简单生活</description>
	<lastBuildDate>Sat, 24 Dec 2011 07:02:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Ruby C++ 扩展</title>
		<link>http://www.jiangmiao.org/blog/2159.html</link>
		<comments>http://www.jiangmiao.org/blog/2159.html#comments</comments>
		<pubDate>Tue, 24 May 2011 18:08:42 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=2159</guid>
		<description><![CDATA[早年写过用C++ + SWIG写Ruby插件的文，但实际中还是以原生C++写Ruby扩展，因为也相当简单。但长久没用还是会忘记，不得不翻以前的老代码回忆，写下这篇博文，若下次再忘记，也不至于去翻仓库。 建立 extconf.rb require &#039;mkmf&#039; $libs = &#039;-lstdc++&#039; create_makefile &#039;foo&#039; 建立 foo.cc #include &#60;ruby.h&#62; VALUE plus(VALUE self, VALUE va, VALUE vb) { int a = NUM2INT(va); int b = NUM2INT(vb); return INT2NUM(a+b); } extern &#34;C&#34; void Init_foo() { &#8230; <a href="http://www.jiangmiao.org/blog/2159.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>早年写过用C++ + SWIG写Ruby插件的文，但实际中还是以原生C++写Ruby扩展，因为也相当简单。但长久没用还是会忘记，不得不翻以前的老代码回忆，写下这篇博文，若下次再忘记，也不至于去翻仓库。</p>
<p>建立 extconf.rb
<div class="pre">
<pre>
require &#039;mkmf&#039;

$libs = &#039;-lstdc++&#039;
create_makefile &#039;foo&#039;
</pre>
</div>
<p>建立 foo.cc
<div class="pre">
<pre>
#include &lt;ruby.h&gt;

VALUE plus(VALUE self, VALUE va, VALUE vb)
{
	int a = NUM2INT(va);
	int b = NUM2INT(vb);

	return INT2NUM(a+b);
}

extern &quot;C&quot; void Init_foo()
{
	VALUE foo = rb_define_module(&quot;Foo&quot;);
	rb_define_module_function(foo, &quot;plus&quot;, RUBY_METHOD_FUNC(plus), 2);
}
</pre>
</div>
<p>生成扩展 foo.so
<div class="pre">
<pre>
$ ruby extconf.rb
$ make
# 如果要安装至site-ruby
$ make site-install
</pre>
</div>
<p>测试文件 test.rb
<div class="pre">
<pre>
require &#039;foo.so&#039;

puts Foo.plus(3,4)
</pre>
</div>
<div class="pre">
<pre>
$ ruby test.rb
7</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/2159.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby下使用SWIG封装libcurl</title>
		<link>http://www.jiangmiao.org/blog/1163.html</link>
		<comments>http://www.jiangmiao.org/blog/1163.html#comments</comments>
		<pubDate>Thu, 02 Sep 2010 05:44:13 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=1163</guid>
		<description><![CDATA[本文通过在ruby下使用SWIG封装libcurl来简要描述如何通过SWIG来扩展ruby。关于SWIG的入门使用可参见 使用SWIG扩展ruby性能 一、关于SWIG SWIG全称为 Simplified Wrapper and Interface Generator，通过swig可以方便快捷的使用c/c++扩展php, ruby等语言以提高性能或满足其它用途。 二、关于libcurl libcurl是一个著名的http/ftp库，通过libcurl可能轻松通过http/ftp协议访问网络。 三、流程 四、相关文件 1. SWIG的封装文件 curl.i %module curl %{ #include &#60;curl/curl.h&#62;; %} /* type definations */ typedef int CURLoption; typedef int CURLcode; /* import constants */ %constant int CURL_GLOBAL_ALL &#8230; <a href="http://www.jiangmiao.org/blog/1163.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>本文通过在ruby下使用SWIG封装libcurl来简要描述如何通过SWIG来扩展ruby。<br />关于SWIG的入门使用可参见 <a href="http://www.jiangmiao.org/blog/268.html">使用SWIG扩展ruby性能</a><br />
<h3>一、关于SWIG</h3>
<p>SWIG全称为 Simplified Wrapper and Interface Generator，通过swig可以方便快捷的使用c/c++扩展php, ruby等语言以提高性能或满足其它用途。<br />
<h3>二、关于libcurl</h3>
<p>libcurl是一个著名的http/ftp库，通过libcurl可能轻松通过http/ftp协议访问网络。<br />
<h3>三、流程</h3>
<p><a href="http://www.jiangmiao.org/blog/wp-content/uploads/2010/09/ruby_swig1.png"><img src="http://www.jiangmiao.org/blog/wp-content/uploads/2010/09/ruby_swig1.png" alt="" title="ruby_swig" width="256" height="347" class="alignnone size-full wp-image-1188" /></a><br />
<h3>四、相关文件</h3>
<h4>1. SWIG的封装文件 curl.i</h4>
<div class="pre">
<pre class="cpp" name="code">%module curl
%{
#include &lt;curl/curl.h&gt;;
%}

/* type definations */
typedef int CURLoption;
typedef int CURLcode;

/* import constants */
%constant int CURL_GLOBAL_ALL = CURL_GLOBAL_ALL;
%constant int CURLOPT_URL = CURLOPT_URL;

/* import functions */
int curl_global_init(long flags);
void curl_global_cleanup();

CURL* curl_easy_init();
void curl_easy_cleanup(CURL* curl);
CURLcode curl_easy_setopt(CURL* curl, CURLoption opt, int v);
CURLcode curl_easy_setopt(CURL* curl, CURLoption opt, const char* v);
CURLcode curl_easy_perform(CURL* curl);</pre>
</div>
<h4>2. ruby的extconf.rb文件</h4>
<div class="pre">
<pre class="ruby" name="code">require &#039;mkmf&#039;
%w{stdc++ curl}.each do|lib|
	$libs = append_library $libs, lib
end
create_makefile &#039;curl&#039;</pre>
</div>
<h4>3. 编译build.sh</h4>
<div class="pre">
<pre class="ruby" name="code">#通过curl.i生成封装后的cpp源码
swig -c++ -ruby curl.i &amp;&amp;

#通过extconf.rb生成Makefile
ruby extconf.rb &amp;&amp;

#通过Makefile生成目标扩展
make</pre>
</div>
<h4>4. 测式文件ruby_test.rb</h4>
<div class="pre">
<pre class="ruby" name="code">#!/usr/bin/env ruby
require &#039;curl&#039;
include Curl

url = ARGV[0] || &#039;http://www.google.com&#039;

p curl_global_init(CURL_GLOBAL_ALL)
p curl =  curl_easy_init()
p curl_easy_setopt(curl, CURLOPT_URL, url)
p curl_easy_perform(curl)
p curl_easy_cleanup(curl)
p curl_global_cleanup()</pre>
</div>
<p>本文相关的完整源码可在github下载:  <a href="http://github.com/JiangMiao/ruby_swig_curl_demo" target="_blank">http://github.com/JiangMiao/ruby_swig_curl_demo</a><br />git clone git://github.com/JiangMiao/ruby_swig_curl_demo.git<br />
<h3>五、相关链接</h3>
<p><a href="http://curl.haxx.se/libcurl/" target="_blank">libcurl</a><br /><a href="http://www.swig.org/" target="_blank">SWIG</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/1163.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby的预定义变量</title>
		<link>http://www.jiangmiao.org/blog/702.html</link>
		<comments>http://www.jiangmiao.org/blog/702.html#comments</comments>
		<pubDate>Tue, 23 Mar 2010 14:15:27 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[1 ruby/基础]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=702</guid>
		<description><![CDATA[一、异常 名称 描述 相当于 $! 捕获的异常 Exception $@ 异常的backtrace Exception#backtrace 例： begin raise &#34;err msg&#34; rescue p $! # #&#38;lt;RuntimeError: err msg&#38;gt; p $@ #[&#34;(irb):2:in `irb_binding&#039;&#34;, &#34;/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding&#039;&#34;, &#34;:0&#34;] end 二、正则 对于 m=str.match(pattern) 变量 性质 描述 相当于 $&#038; 只读,本地 匹配的项 &#8230; <a href="http://www.jiangmiao.org/blog/702.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>一、异常</h3>
<table>
<tr>
<th width="33.33%">名称</th>
<th width="33.33%">描述</th>
<th width="33.33%">相当于</th>
</tr>
<tr>
<td width="33.33%">$!</td>
<td width="33.33%">捕获的异常</td>
<td width="33.33%">Exception</td>
</tr>
<tr>
<td width="33.33%">$@</td>
<td width="33.33%">异常的backtrace</td>
<td width="33.33%">Exception#backtrace</td>
</tr>
</table>
<p>例：
<div class="pre">
<pre class="ruby" name="code">begin
	raise &quot;err msg&quot;
rescue
	p $! # #&amp;lt;RuntimeError: err msg&amp;gt;
	p $@ #[&quot;(irb):2:in `irb_binding&#039;&quot;, &quot;/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding&#039;&quot;, &quot;:0&quot;]
end</pre>
</div>
<h3>二、正则</h3>
<p>对于 m=str.match(pattern)<br />
<table>
<tr>
<th width="25%">变量</th>
<th width="25%">性质</th>
<th width="25%">描述</th>
<th width="25%">相当于</th>
</tr>
<tr>
<td width="25%">$&#038;</td>
<td width="25%">只读,本地</td>
<td width="25%">匹配的项</td>
<td width="25%">m[0]</td>
</tr>
<tr>
<td width="25%">$`</td>
<td width="25%">只读,本地</td>
<td width="25%">匹配的字符串之前未匹配的部分</td>
<td width="25%">m.pre_match</td>
</tr>
<tr>
<td width="25%">$&#8217;</td>
<td width="25%">只读,本地</td>
<td width="25%">匹配的字符串之后未匹配的部分</td>
<td width="25%">m.post_match</td>
</tr>
<tr>
<td width="25%">$+</td>
<td width="25%">只读,本地</td>
<td width="25%">最后一个匹配的项</td>
<td width="25%">m[m.size-1]</td>
</tr>
<tr>
<td width="25%">$1,$2</td>
<td width="25%">只读,本地</td>
<td width="25%">所指定的匹配的项</td>
<td width="25%">m[n]</td>
</tr>
<tr>
<td width="25%">$~</td>
<td width="25%">本地</td>
<td width="25%">匹配的结果</td>
<td width="25%">m</td>
</tr>
</table>
<p>例:
<div class="pre">
<pre class="ruby" name="code">&quot;start-111-222-333-444-end&quot; =~ /(\d+)-(\d+)-(\d+)-(\d+)/
p $&amp; #&quot;111-222-333-444&quot;
p $` # &quot;start-&quot;
p $&#039; # &quot;-end&quot;
p $+ # &quot;444&quot;
p $1 # &quot;111&quot;
p $2 # &quot;222&quot;
p $~[3] # &quot;333&quot;</pre>
</div>
<h3>三、IO/String</h3>
<table>
<tr>
<th width="33.33%">名称</th>
<th width="33.33%">描述</th>
<th width="33.33%">默认</th>
</tr>
<tr>
<td width="33.33%">$_</td>
<td width="33.33%">gets,readline最后读入的行</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$F</td>
<td width="33.33%">$_.split 仅当选项-a为真时</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$/</td>
<td width="33.33%">用于gets,readline等分割符</td>
<td width="33.33%">&#8220;\n&#8221;</td>
</tr>
<tr>
<td width="33.33%">$\</td>
<td width="33.33%">print与IO.write的分割符</td>
<td width="33.33%">nil</td>
</tr>
<tr>
<td width="33.33%">$,</td>
<td width="33.33%">Array#join的分割符</td>
<td width="33.33%">nil</td>
</tr>
<tr>
<td width="33.33%">$;</td>
<td width="33.33%">String#split的分割符</td>
<td width="33.33%">nil</td>
</tr>
<tr>
<td width="33.33%">$.</td>
<td width="33.33%">ARGF.lineno</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$<</td>
<td width="33.33%">ARGF</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$*</td>
<td width="33.33%">ARGV</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$></td>
<td width="33.33%">$defout</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$defout</td>
<td width="33.33%">print与printf的输出变量，缺省为$stdio</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$stdin</td>
<td width="33.33%">当前标准输入 缺省 STDIN</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$stdout</td>
<td width="33.33%">当前标准输出 缺省 STDOUT</td>
<td width="33.33%"></td>
</tr>
<tr>
<td width="33.33%">$stderr</td>
<td width="33.33%">当前标准错误输出 缺省 STDERR</td>
<td width="33.33%"></td>
</tr>
</table>
<h3>四、进程</h3>
<table>
<tr>
<th width="50%">名称</th>
<th width="50%">描述</th>
</tr>
<tr>
<td width="50%">$0</td>
<td width="50%">当前ruby程序名</td>
</tr>
<tr>
<td width="50%">$$</td>
<td width="50%">Process.pid</td>
</tr>
<tr>
<td width="50%">$?</td>
<td width="50%">最后执行的外部程序的返回状态</td>
</tr>
</table>
<h3>五、其它</h3>
<table>
<tr>
<th width="50%">名称</th>
<th width="50%">描述</th>
</tr>
<tr>
<td width="50%">$:</td>
<td width="50%">$LOAD_PATH</td>
</tr>
<tr>
<td width="50%">$&#8221;</td>
<td width="50%">已require的module，用于避免二次载入</td>
</tr>
<tr>
<td width="50%">$DEBUG</td>
<td width="50%">是否为DEBUG状态, -d &#8211;debug开启则为真</td>
</tr>
<tr>
<td width="50%">$FILENAME</td>
<td width="50%">$<.filename</td>
</tr>
<tr>
<td width="50%">$SAFE</td>
<td width="50%">安全等级</td>
</tr>
<tr>
<td width="50%">$VERBOSE</td>
<td width="50%">verbose选项是开启则为真</td>
</tr>
<tr>
<td width="50%">$-O</td>
<td width="50%">$/</td>
</tr>
<tr>
<td width="50%">$-a</td>
<td width="50%">-a选项开启则为真</td>
</tr>
<tr>
<td width="50%">$-d</td>
<td width="50%">$DEBUG</td>
</tr>
<tr>
<td width="50%">$-F</td>
<td width="50%">$;</td>
</tr>
<tr>
<td width="50%">$-I</td>
<td width="50%">$:</td>
</tr>
<tr>
<td width="50%">$-l</td>
<td width="50%">-lis选项开启则为真</td>
</tr>
<tr>
<td width="50%">$-p</td>
<td width="50%">-pis选项开启则为真</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/702.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何编写RubyGems</title>
		<link>http://www.jiangmiao.org/blog/639.html</link>
		<comments>http://www.jiangmiao.org/blog/639.html#comments</comments>
		<pubDate>Sun, 21 Mar 2010 09:50:51 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[1 ruby/基础]]></category>
		<category><![CDATA[4 rubygems]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=639</guid>
		<description><![CDATA[目标：通过本文章学习并编写一个gem &#8216;hello&#8217; 一、认识RubyGems 1、什么是RubyGems RubyGems是ruby下的包管理系统。最著名gem的当属Rails 2、gem的基本文件布局 文件名 类别 描述 gemspec 文件 gem的规范说明。 lib 文件夹 用于lib文件的存放 bin 文件夹 可执行文件 ext 文件夹 c/c++源文件 tests 文件夹 单元测试文件 3、gemspec文件规范常用项 名称 类别 缺省 描述 name String gem名称 *version String 版本号 *date Time Time.now Gem创建日期 *platform &#8230; <a href="http://www.jiangmiao.org/blog/639.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>目标：通过本文章学习并编写一个gem &#8216;hello&#8217;<br />
<h3>一、认识RubyGems</h3>
<h4>1、什么是RubyGems</h4>
<p>RubyGems是ruby下的包管理系统。最著名gem的当属Rails<br />
<h4>2、gem的基本文件布局</h4>
<table>
<tr>
<td width="33.33%">文件名</td>
<td width="33.33%">类别</td>
<td width="33.33%">描述</td>
</tr>
<tr>
<td width="33.33%">gemspec</td>
<td width="33.33%">文件</td>
<td width="33.33%">gem的规范说明。</td>
</tr>
<tr>
<td width="33.33%">lib</td>
<td width="33.33%">文件夹</td>
<td width="33.33%">用于lib文件的存放</td>
</tr>
<tr>
<td width="33.33%">bin</td>
<td width="33.33%">文件夹</td>
<td width="33.33%">可执行文件</td>
</tr>
<tr>
<td width="33.33%">ext</td>
<td width="33.33%">文件夹</td>
<td width="33.33%">c/c++源文件</td>
</tr>
<tr>
<td width="33.33%">tests</td>
<td width="33.33%">文件夹</td>
<td width="33.33%">单元测试文件</td>
</tr>
</table>
<h4>3、gemspec文件规范常用项</h4>
<table>
<tr>
<td width="25%">名称</td>
<td width="25%">类别</td>
<td width="25%">缺省</td>
<td width="25%">描述</td>
</tr>
<tr>
<td width="25%">name</td>
<td width="25%">String</td>
<td width="25%"></td>
<td width="25%">gem名称</td>
</tr>
<tr>
<td width="25%">*version</td>
<td width="25%">String</td>
<td width="25%"></td>
<td width="25%">版本号</td>
</tr>
<tr>
<td width="25%">*date</td>
<td width="25%">Time</td>
<td width="25%">Time.now</td>
<td width="25%">Gem创建日期</td>
</tr>
<tr>
<td width="25%">*platform</td>
<td width="25%">String</td>
<td width="25%">Gem::Platform::Ruby</td>
<td width="25%">gems所使用的平台</td>
</tr>
<tr>
<td width="25%">*summary</td>
<td width="25%">String</td>
<td width="25%"></td>
<td width="25%">gem描述</td>
</tr>
<tr>
<td width="25%">*require_paths</td>
<td width="25%">Array</td>
<td width="25%">["lib"]</td>
<td width="25%">用于require调用时的默认路径</td>
</tr>
<tr>
<td width="25%">files</td>
<td width="25%">Array</td>
<td width="25%"></td>
<td width="25%">gems所包含的文件</td>
</tr>
</table>
<p>以上为关键的几个选项，其中打*号的为&#8221;&#8217;必须&#8221;&#8217;的项，若要添加作者，可执行文件等选项可参见<a href="http://docs.rubygems.org/read/chapter/20" target="_blank">Gem规范</a><br />
<h4>4、编译</h4>
<p>gem build gemspec文件<br />
<h4>5、安装</h4>
<p>gem install 生成的gem<br />
<h3>二、编写最简gem hello</h3>
<h4>1、创建文件与文件夹</h4>
<p>hello/hello.gemspec<br />hello/lib/hello.rb<br />
<h4>2、编写 hello.gemspec</h4>
<pre class="ruby" name="code">
Gem::Specification.new do |s|
	s.name = 'hello'
	s.version = '0.1.0'
	s.summary = 'hello gems'
	s.files = ["lib/hello.rb"]
end
</pre>
<h4>3、编写 lib/hello.rb</h4>
<pre class="ruby" name="code">
def hello
	"HELLO"
end
</pre>
<h4>4、编译</h4>
<blockquote><p>gem build hello.gemspec</p></blockquote>
<h4>5、安装</h4>
<blockquote><p>gem install hello</p></blockquote>
<h4>6、测试</h4>
<pre class="ruby" name="code">
require 'rubygems'
require 'hello'
puts hello
</pre>
<p>&#8212;-<br />输出HELLO 测试成功<br />
<h3>三、相关链接</h3>
<p>1、<a href="http://rubygems.org/" target="_blank">RubyGems官方网站</a><br />2、<a href="http://rubyforge.org" target="_blank">Rubyforge</a><br />2、<a href="http://docs.rubygems.org/read/chapter/20" target="_blank">Gem规范</a><br />3、<a href="http://www.jiangmiao.org/blog/wp-content/uploads/2010/03/hello.tar.gz">本文的hello gem源码</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/639.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>从rails回到了php</title>
		<link>http://www.jiangmiao.org/blog/367.html</link>
		<comments>http://www.jiangmiao.org/blog/367.html#comments</comments>
		<pubDate>Wed, 11 Feb 2009 20:41:01 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=367</guid>
		<description><![CDATA[从rails回来，重新用起了php，起因还是由于rails render一个63k的view要多花去10ms，这个view就一个erb fragment缓存，，即由原来每个响应20ms下降到30ms，降低了足足50%，很受伤，后然尝试使用merb，但merb不太习惯，正值ruby1.9.1,Rails2.3发页，又尝试ruby1.9.1，可惜很多gems都未能完善，fcgi,mongrel都无法工作，只能用回了1.8.7。最后无奈之下用回了php。用php重写了所有代码后，一模一样的操作那个63k的页面只需10ms。  但rails给我的启发是巨大的，现在我的目录结构也仿rails, 如app,config,public,log,tmp等。我甚至写了个Rakefile用来管理文件的常用操作，使用了ruby的2个月。学到了不少。ruby我还会继续用他，作为我的刀，希望ruby越来越好，早一天让我从php又回到了ruby。]]></description>
			<content:encoded><![CDATA[<p>从rails回来，重新用起了php，起因还是由于rails render一个63k的view要多花去10ms，这个view就一个erb fragment缓存，，即由原来每个响应20ms下降到30ms，降低了足足50%，很受伤，后然尝试使用merb，但merb不太习惯，正值ruby1.9.1,Rails2.3发页，又尝试ruby1.9.1，可惜很多gems都未能完善，fcgi,mongrel都无法工作，只能用回了1.8.7。最后无奈之下用回了php。用php重写了所有代码后，一模一样的操作那个63k的页面只需10ms。 </p>
<p>但rails给我的启发是巨大的，现在我的目录结构也仿rails, 如app,config,public,log,tmp等。我甚至写了个Rakefile用来管理文件的常用操作，使用了ruby的2个月。学到了不少。ruby我还会继续用他，作为我的刀，希望ruby越来越好，早一天让我从php又回到了ruby。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/367.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ruby稳定版已更新为1.9.1-p0</title>
		<link>http://www.jiangmiao.org/blog/363.html</link>
		<comments>http://www.jiangmiao.org/blog/363.html#comments</comments>
		<pubDate>Tue, 10 Feb 2009 12:59:03 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[5 ruby/其它]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=363</guid>
		<description><![CDATA[今天发现stable version已从1.8.7升至为1.9.1-p0。一会测试一下兼容性和性能的提升。 相关链接Ruby官方网站 &#8211;2009-02-10补充&#8211;到正式可以用至少还要1个月，许许多多gems都还未能兼容1.9.1。部份版本已出了新版本，但都还在github中。ruby的脚步太快， 追起来有点吃力，暂时驻足观望一阵。]]></description>
			<content:encoded><![CDATA[<p>今天发现stable version已从1.8.7升至为1.9.1-p0。一会测试一下兼容性和性能的提升。</p>
<p>相关链接<br /><a href="http://www.ruby-lang.org/en/downloads/" target="_blank">Ruby官方网站</a></p>
<p>&#8211;2009-02-10补充&#8211;<br />到正式可以用至少还要1个月，许许多多gems都还未能兼容1.9.1。部份版本已出了新版本，但都还在github中。<br />ruby的脚步太快， 追起来有点吃力，暂时驻足观望一阵。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/363.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rb_yield让ruby/c++更加紧密</title>
		<link>http://www.jiangmiao.org/blog/320.html</link>
		<comments>http://www.jiangmiao.org/blog/320.html#comments</comments>
		<pubDate>Mon, 26 Jan 2009 10:54:00 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=320</guid>
		<description><![CDATA[用c++写ruby模块提升性能效果显著一文让我初尝甜头，但未免连render都用c++也太得不尝失。自从使用了rb_yield，一切问题迎刃而解。 如代码 class Test def foo yield end end 相对应的c++代码则为 #include "ruby.h" typedef VALUE(*RUBY_FUNC)(...); extern "C" VALUE t_foo(VALUE self) { //do something by c++ rb_yield(); //do something by c++ } VALUE cTest = rb_define_class("Test",rb_cObject); rb_define_method(cTest,"foo",(RUBY_FUNC)t_foo,1); 非常的方便有效。]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jiangmiao.org/blog/295.html">用c++写ruby模块提升性能效果显著</a>一文让我初尝甜头，但未免连render都用c++也太得不尝失。自从使用了rb_yield，一切问题迎刃而解。</p>
<p>如代码
<pre class="ruby" name="code">
class Test
  def foo
    yield
  end
end
</pre>
<p>相对应的c++代码则为
<pre class="cpp" name="code">#include "ruby.h"
typedef VALUE(*RUBY_FUNC)(...);
extern "C" VALUE t_foo(VALUE self)
{
  //do something by c++
  rb_yield();
  //do something by c++
}
VALUE cTest = rb_define_class("Test",rb_cObject);
rb_define_method(cTest,"foo",(RUBY_FUNC)t_foo,1);</pre>
<p>非常的方便有效。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/320.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用c++写ruby模块提升性能效果显著</title>
		<link>http://www.jiangmiao.org/blog/295.html</link>
		<comments>http://www.jiangmiao.org/blog/295.html#comments</comments>
		<pubDate>Sat, 17 Jan 2009 13:13:01 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=295</guid>
		<description><![CDATA[对于生成600项item的树型目录。纯ruby代码大约200ms包括rails初始化(0-6ms)，mysql查询(0-2ms),树型生成(20-30ms)，递归render，其它。 经过c++重写后，全部只需10ms。实在太爽啦。如果那递归render都用c++代替，那感觉是用C++写网页，而不是用ruby写网页，利用不到rails提供的很多特性。但递归render 600项实在耗时，鱼和熊掌不能兼得，得另想法子。]]></description>
			<content:encoded><![CDATA[<div class="noindent">对于生成600项item的树型目录。<br />纯ruby代码大约200ms<br />包括rails初始化(0-6ms)，mysql查询(0-2ms),树型生成(20-30ms)，递归render，其它。</p>
<p>经过c++重写后，全部只需10ms。实在太爽啦。<br />如果那递归render都用c++代替，那感觉是用C++写网页，而不是用ruby写网页，利用不到rails提供的很多特性。但递归render 600项实在耗时，鱼和熊掌不能兼得，得另想法子。</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/295.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用SWIG扩展ruby性能</title>
		<link>http://www.jiangmiao.org/blog/268.html</link>
		<comments>http://www.jiangmiao.org/blog/268.html#comments</comments>
		<pubDate>Fri, 16 Jan 2009 10:09:13 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=268</guid>
		<description><![CDATA[　　在使用ruby/rails的过程中，确实发现有时性能不尽人意，如生成一个拥有600项的item的3层树形结构目录要花去20ms，为提高性能在学习用c/c++写ruby模块的过程中，认识了swig，rubyInline等一系列帮助编写c/c++来提升ruby性能的辅助工具。　　rubyInline用于内嵌c/c++程序，简单快捷，　　swig则帮助我们更容易地用c/c++写出独立的ruby模块。  swig的入门使用方法目标：用swig/c++编写一个ruby模块Test,并提供add方法作加法运算。相关文件： test.i 接口 test.h 头文件 test.cxx 函数实现 extconf.rb 用于生成makefile (自动)test_wrap.cxx swig生成的test封装 (自动)Makefile Makefile文件由ruby extconf.rb得到 (自动)test.so ruby模块 由make得到 1、建立接口文件test.i %module test %{ //包含头文件 #include "test.h" %} //接口add int add(int,int); 2、编写wrap文件 swig -c++ -ruby test.i 得到test封装文件 test_wrap.cxx 3、编写test.h与test.cxx //test.h #ifndef _TEST_TEST_H &#8230; <a href="http://www.jiangmiao.org/blog/268.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="noindent">　　在使用ruby/rails的过程中，确实发现有时性能不尽人意，如生成一个拥有600项的item的3层树形结构目录要花去20ms，为提高性能在学习用c/c++写ruby模块的过程中，认识了swig，rubyInline等一系列帮助编写c/c++来提升ruby性能的辅助工具。<br />　　rubyInline用于内嵌c/c++程序，简单快捷，<br />　　swig则帮助我们更容易地用c/c++写出独立的ruby模块。 </p>
<p><strong>swig的入门使用方法</strong><br />目标：用swig/c++编写一个ruby模块Test,并提供add方法作加法运算。<br />相关文件：
<ul>
<li>test.i 接口</li>
<li>test.h 头文件</li>
<li>test.cxx 函数实现</li>
<li>extconf.rb 用于生成makefile</li>
<li>(自动)test_wrap.cxx  swig生成的test封装</li>
<li>(自动)Makefile Makefile文件由ruby extconf.rb得到</li>
<li>(自动)test.so ruby模块 由make得到</li>
</ul>
<p><strong>1、建立接口文件test.i</strong>
<pre>%module test
%{
//包含头文件
#include "test.h"
%}
//接口add
int add(int,int);</pre>
<p><strong>2、编写wrap文件</strong>
<pre>swig -c++ -ruby test.i</pre>
<p>得到test封装文件 test_wrap.cxx</p>
<p><strong>3、编写test.h与test.cxx</strong>
<pre class="cpp">//test.h
#ifndef _TEST_TEST_H
#define _TEST_TEST_H
extern int add(int,int);
#endif</pre>
<pre class="cpp">//test.cxx
#include "test.h"
int add(int left,int right)
{
        return left+right;
}</pre>
<p><strong>4、编写extconf.rb用于快速生成makefile</strong>
<pre class="ruby">require 'mkmf'

dir_config 'test'
#stdc++库，add函数未用到
$libs = append_library $libs,'stdc++'
create_makefile 'test'</pre>
<p>运行 ruby extconf.rb 得到 Makefile 文件</p>
<p><strong>5、生成test模块</strong><br />运行 make 得到模块 test.so</p>
<p><strong>6、测试</strong>
<pre>irb
irb(main):001:0&gt; require 'test'
=&gt; true
irb(main):002:0&gt; Test.add 3,4
=&gt; 7
irb(main):003:0&gt; Test.add 3333333333333333333333,44444444444444444
TypeError: Expected argument 0 of type int, but got Bignum 3333333333333333333333
        in SWIG method 'add'
        from (irb):3:in `add'
        from (irb):3
        from :0
irb(main):004:0&gt;</pre>
<p>测试成功</p>
<p><strong>7、swig</strong><br />swig支持很多c++的高级特性来编写ruby的模块，如类，继承，重载，模板，stl等。</p>
<p><strong>8、相关链接</strong>
<ul>
<li><a href="http://www.swig.org/" target="_blank">swig</a></li>
<li><a href="http://www.swig.org/Doc1.3/Ruby.html" target="_blank">swig/ruby 文档</a></li>
<li><a href="http://www.jiangmiao.org/blog" target="_blank">作者的blog</a></li>
</ul>
<p><strong>9、备注</strong><br />本文的add函数过于简单，对比ruby 3+4性能不升反降。</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/268.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 使用Haml代替Rhtml</title>
		<link>http://www.jiangmiao.org/blog/254.html</link>
		<comments>http://www.jiangmiao.org/blog/254.html#comments</comments>
		<pubDate>Wed, 31 Dec 2008 18:08:24 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[2 ruby/rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=254</guid>
		<description><![CDATA[什么是HamlHaml，一款简洁的的类似Rhtml的插件，可以极大的简少写xhtml等视图代码的量。 安装haml安装gemsudo gem install haml安装haml plugin至railshaml &#8211;rails [rails路径] 如 haml &#8211;rails . 则安装haml插件至当前目录 第一个haml %h1 Chapters#index %p == Time: #{Time.now} =h "a~!@\#$%^&#038;*()+-" 输出 &#60;h1>Chapters#index&#60;/h1> &#60;p> Time: Thu Jan 01 01:56:57 +0800 2009 a~!@#$%^&#38;amp;*()+- &#60;/p> 比起笨重的Rhtml实在是简化不少。 性能rhtml 158.72 reqs/shaml 154.83 reqs/s下降约 &#8230; <a href="http://www.jiangmiao.org/blog/254.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="noindent"><strong>什么是Haml</strong><br />Haml，一款简洁的的类似Rhtml的插件，可以极大的简少写xhtml等视图代码的量。</p>
<p><strong>安装haml</strong><br />安装gem<br />sudo gem install haml<br />安装haml plugin至rails<br />haml &#8211;rails [rails路径] 如 haml &#8211;rails . 则安装haml插件至当前目录</p>
<p><strong>第一个haml</strong>
<pre>
%h1 Chapters#index
%p
  == Time: #{Time.now}
  =h "a~!@\#$%^&#038;*()+-"
</pre>
<p>输出
<pre>
&lt;h1>Chapters#index&lt;/h1>
&lt;p>
  Time: Thu Jan 01 01:56:57 +0800 2009
  a~!@#$%^&amp;amp;*()+-
&lt;/p>
</pre>
<p>比起笨重的Rhtml实在是简化不少。</p>
<p><strong>性能</strong><br />rhtml 158.72 reqs/s<br />haml 154.83 reqs/s<br />下降约 2.45%<br />单个文件请求时间绝对值增加 0.159ms<br />对性能的影响还是微乎其微的</p>
<p><strong>相关链接</strong><br /><a href="http://haml.hamptoncatlin.com/" target="_blank">haml首页</a><br /><a href="http://haml.hamptoncatlin.com/tutorial/" target="_blank">Haml Tutorial</a><br /><a href="http://haml.hamptoncatlin.com/docs/" target="_blank">Haml Documentation</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/254.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

