<?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>Wed, 08 Sep 2010 05:13:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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协议访问网络。 流程: 相关文件: 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 = CURL_GLOBAL_ALL; %constant int CURLOPT_URL = CURLOPT_URL; /* import functions */ int curl_global_init(long flags); void [...]]]></description>
			<content:encoded><![CDATA[<p>本文通过在ruby下使用SWIG封装libcurl来简要描述如何通过SWIG来扩展ruby。<br />
关于SWIG的入门使用可参见 <a href="http://www.jiangmiao.org/blog/268.html">使用SWIG扩展ruby性能</a></p>
<p><strong>关于SWIG:</strong><br />
SWIG全称为 Simplified Wrapper and Interface Generator，通过swig可以方便快捷的使用c/c++扩展php, ruby等语言以提高性能或满足其它用途。</p>
<p><strong>关于libcurl:</strong><br />
libcurl是一个著名的http/ftp库，通过libcurl可能轻松通过http/ftp协议访问网络。</p>
<p><strong>流程:</strong><br />
<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></p>
<p><strong>相关文件:</strong><br />
<strong>SWIG的封装文件 curl.i</strong></p>
<pre name="code" class="cpp">%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>
<p><strong>ruby的extconf.rb文件</strong></p>
<pre name="code" class="ruby">require 'mkmf'
%w{stdc++ curl}.each do|lib|
	$libs = append_library $libs, lib
end
create_makefile 'curl'
</pre>
<p><strong>编译build.sh:</strong></p>
<pre name="code" class="ruby">#通过curl.i生成封装后的cpp源码
swig -c++ -ruby curl.i &#038;&#038;

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

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

url = ARGV[0] || 'http://www.google.com'

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>
<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</p>
<p><strong>相关链接:</strong><br />
<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[一、异常 名称 描述 相当于 &#36;! 捕获的异常 Exception &#36;@ 异常的backtrace Exception#backtrace 例： begin raise "err msg" rescue p &#36;! # #&#60;RuntimeError: err msg&#62; p &#36;@ #["(irb):2:in `irb_binding'", "/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"] end 二、正则 对于 m=str.match(pattern) 变量 性质 描述 相当于 &#36;&#038; 只读,本地 匹配的项 m[0] &#36;` 只读,本地 匹配的字符串之前未匹配的部分 m.pre_match &#36;\&#8217; 只读,本地 匹配的字符串之后未匹配的部分 m.post_match &#36;+ 只读,本地 最后一个匹配的项 m[m.size-1] &#36;1,&#36;2 只读,本地 [...]]]></description>
			<content:encoded><![CDATA[<p><span class="easyformat"><b>一、异常</b></p>
<table >
<tr>
<th>名称</th>
<th>描述</th>
<th>相当于</th>
</tr>
<tr >
<td>&#36;!</td>
<td>捕获的异常</td>
<td>Exception</td>
</tr>
<tr >
<td>&#36;@</td>
<td>异常的backtrace</td>
<td>Exception#backtrace</td>
</tr>
</table>
<p>例：</p>
<pre class="ruby" name="code">
begin
	raise "err msg"
rescue
	p &#36;! # #&lt;RuntimeError: err msg&gt;
	p &#36;@ #["(irb):2:in `irb_binding'", "/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"]
end
</pre>
<p><b>二、正则</b><br />
对于 m=str.match(pattern)</p>
<table >
<tr>
<th>变量</th>
<th>性质</th>
<th>描述</th>
<th>相当于</th>
</tr>
<tr >
<td>&#36;&#038;</td>
<td>只读,本地</td>
<td>匹配的项</td>
<td>m[0]</td>
</tr>
<tr >
<td>&#36;`</td>
<td>只读,本地</td>
<td>匹配的字符串之前未匹配的部分</td>
<td>m.pre_match</td>
</tr>
<tr >
<td>&#36;\&#8217;</td>
<td>只读,本地</td>
<td>匹配的字符串之后未匹配的部分</td>
<td>m.post_match</td>
</tr>
<tr >
<td>&#36;+</td>
<td>只读,本地</td>
<td>最后一个匹配的项</td>
<td>m[m.size-1]</td>
</tr>
<tr >
<td>&#36;1,&#36;2</td>
<td>只读,本地</td>
<td>所指定的匹配的项</td>
<td>m[n]</td>
</tr>
<tr >
<td>&#36;~</td>
<td>本地</td>
<td>匹配的结果</td>
<td>m</td>
</tr>
</table>
<p>例:</p>
<pre class="ruby" name="code">
"start-111-222-333-444-end" =~ /(\d+)-(\d+)-(\d+)-(\d+)/
p &#36;&#038; #"111-222-333-444"
p &#36;` # "start-"
p &#36;' # "-end"
p &#36;+ # "444"
p &#36;1 # "111"
p &#36;2 # "222"
p &#36;~[3] # "333"
</pre>
<p><b>三、IO/String</b></p>
<table >
<tr>
<th>描述</th>
<th>默认</th>
<th>说明</th>
</tr>
<tr >
<td>&#36;_</td>
<td></td>
<td>gets,readline最后读入的行</td>
</tr>
<tr >
<td>&#36;F</td>
<td></td>
<td>&#36;_.split 仅当选项-a为真时</td>
</tr>
<tr >
<td>&#36;/</td>
<td>&#8220;\n&#8221;</td>
<td>用于gets,readline等分割符</td>
</tr>
<tr >
<td>&#36;\</td>
<td>nil</td>
<td>print与IO.write的分割符</td>
</tr>
<tr >
<td>&#36;,</td>
<td>nil</td>
<td>Array#join的分割符</td>
</tr>
<tr >
<td>&#36;;</td>
<td>nil</td>
<td>String#split的分割符</td>
</tr>
<tr >
<td>&#36;.</td>
<td></td>
<td>ARGF.lineno</td>
</tr>
<tr >
<td>&#36;<</td>
<td></td>
<td>ARGF</td>
</tr>
<tr >
<td>&#36;*</td>
<td></td>
<td>ARGV</td>
</tr>
<tr >
<td>&#36;></td>
<td></td>
<td>&#36;defout</td>
</tr>
<tr >
<td>&#36;defout</td>
<td></td>
<td>print与printf的输出变量，缺省为&#36;stdio</td>
</tr>
<tr >
<td>&#36;stdin</td>
<td></td>
<td>当前标准输入 缺省 STDIN</td>
</tr>
<tr >
<td>&#36;stdout</td>
<td></td>
<td>当前标准输出 缺省 STDOUT</td>
</tr>
<tr >
<td>&#36;stderr</td>
<td></td>
<td>当前标准错误输出 缺省 STDERR</td>
</tr>
</table>
<p><b>四、进程</b></p>
<table >
<tr>
<th>名称</th>
<th>描述</th>
</tr>
<tr >
<td>&#36;0</td>
<td>当前ruby程序名</td>
</tr>
<tr >
<td>&#36;&#36;</td>
<td>Process.pid</td>
</tr>
<tr >
<td>&#36;?</td>
<td>最后执行的外部程序的返回状态</td>
</tr>
</table>
<p><b>四、其它</b></p>
<table >
<tr>
<th>名称</th>
<th>描述</th>
</tr>
<tr >
<td>&#36;:</td>
<td>&#36;LOAD_PATH</td>
</tr>
<tr >
<td>&#36;&#8221;</td>
<td>已require的module，用于避免二次载入</td>
</tr>
<tr >
<td>&#36;DEBUG</td>
<td>是否为DEBUG状态, -d &#8211;debug开启则为真</td>
</tr>
<tr >
<td>&#36;FILENAME</td>
<td>&#36;<.filename</td>
</tr>
<tr >
<td>&#36;SAFE</td>
<td>安全等级</td>
</tr>
<tr >
<td>&#36;VERBOSE</td>
<td>verbose选项是开启则为真</td>
</tr>
<tr >
<td>&#36;-O</td>
<td>&#36;/</td>
</tr>
<tr >
<td>&#36;-a</td>
<td>-a选项开启则为真</td>
</tr>
<tr >
<td>&#36;-d</td>
<td>&#36;DEBUG</td>
</tr>
<tr >
<td>&#36;-F</td>
<td>&#36;;</td>
</tr>
<tr >
<td>&#36;-I</td>
<td>&#36;:</td>
</tr>
<tr >
<td>&#36;-l</td>
<td>-lis选项开启则为真</td>
</tr>
<tr >
<td>&#36;-p</td>
<td>-pis选项开启则为真</td>
</tr>
</table>
<p></span></p>
]]></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 String Gem::Platform::Ruby gems所使用的平台 *summary String gem描述 *require_paths Array ["lib"] 用于require调用时的默认路径 files Array gems所包含的文件 以上为关键的几个选项，其中打*号的为必须的项，若要添加作者，可执行文件等选项可参见Gem规范 3、编译 [...]]]></description>
			<content:encoded><![CDATA[<p>目标：通过本文章学习并编写一个gem &#8216;hello&#8217;</p>
<p><span class="easyformat"><b>一、认识RubyGems</b><br />
<b>1、什么是RubyGems</b><br />
RubyGems是ruby下的包管理系统。最著名gem的当属Rails</p>
<p><b>2、gem的基本文件布局</b></p>
<table >
<tr>
<td>文件名</td>
<td>类别</td>
<td>描述</td>
</tr>
<tr >
<td>gemspec</td>
<td>文件</td>
<td>gem的规范说明。</td>
</tr>
<tr >
<td>lib</td>
<td>文件夹</td>
<td>用于lib文件的存放</td>
</tr>
<tr >
<td>bin</td>
<td>文件夹</td>
<td>可执行文件</td>
</tr>
<tr >
<td>ext</td>
<td>文件夹</td>
<td>c/c++源文件</td>
</tr>
<tr >
<td>tests</td>
<td>文件夹</td>
<td>单元测试文件</td>
</tr>
</table>
<p><b>3、gemspec文件规范常用项</b></p>
<table >
<tr>
<td>名称</td>
<td>类别</td>
<td>缺省</td>
<td>描述</td>
</tr>
<tr >
<td>*name</td>
<td>String</td>
<td></td>
<td>gem名称</td>
</tr>
<tr >
<td>*version</td>
<td>String</td>
<td></td>
<td>版本号</td>
</tr>
<tr >
<td>*date</td>
<td>Time</td>
<td>Time.now</td>
<td>Gem创建日期</td>
</tr>
<tr >
<td>*platform</td>
<td>String</td>
<td>Gem::Platform::Ruby</td>
<td>gems所使用的平台</td>
</tr>
<tr >
<td>*summary</td>
<td>String</td>
<td></td>
<td>gem描述</td>
</tr>
<tr >
<td>*require_paths</td>
<td>Array</td>
<td>["lib"]</td>
<td>用于require调用时的默认路径</td>
</tr>
<tr >
<td>files</td>
<td>Array</td>
<td></td>
<td>gems所包含的文件</td>
</tr>
</table>
<p>以上为关键的几个选项，其中打*号的为<b>必须</b>的项，若要添加作者，可执行文件等选项可参见<a href="http://docs.rubygems.org/read/chapter/20" target="_blank">Gem规范</a></p>
<p><b>3、编译</b><br />
gem build gemspec文件</p>
<p><b>4、安装</b><br />
gem install 生成的gem</p>
<p><b>二、编写最简gem hello</b><br />
<b>1、创建文件与文件夹</b><br />
hello/hello.gemspec<br />
hello/lib/hello.rb</p>
<p><b>2、编写 hello.gemspec</b></p>
<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>
<p><b>3、编写 lib/hello.rb</b></p>
<pre class="ruby" name="code">
def hello
	"HELLO"
end
</pre>
<p><b>4、编译</b></p>
<blockquote><p>
gem build hello.gemspec
</p></blockquote>
<p><b>5、安装</b></p>
<blockquote><p>
gem install hello
</p></blockquote>
<p><b>6、测试</b></p>
<pre class="ruby" name="code">
require 'rubygems'
require 'hello'
puts hello
</pre>
<p>&#8212;-<br />
输出HELLO 测试成功</p>
<p><b>三、相关链接</b><br />
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></span></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>1</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>如代码</p>
<pre class="ruby" name="code">
class Test
  def foo
    yield
  end
end
</pre>
<p>相对应的c++代码则为</p>
<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>ruby升到1.8.7，性能提升到我吐血。</title>
		<link>http://www.jiangmiao.org/blog/326.html</link>
		<comments>http://www.jiangmiao.org/blog/326.html#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:49:36 +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=326</guid>
		<description><![CDATA[早听说debian/ubuntu的1.8.6是debug模式，比正常的慢50%，一直没在意，反正是测试服务器不缺这点时间。 前几篇日志提到的600个节点的树递归生成时间与render耗时太长，要近200ms，c++render虽然快但不实用，于是今重写了扩展对erb进行yield，但最终只能优化到70ms(3ms树生成+70ms render)，最后发现即使什么都不作进行600次link_to操作也要花掉60-80ms。随抱着试试的心态升到了1.8.7 升级后发现目录也改成像freebsd的/usr/local/&#8230;的样子不太熟悉。然后安装gem又折腾了一阵。最后怀着激动的心情安装好一试，！只要20-40ms！又试了试其它页面，原本要花0-6ms的页面全都稳定得变为0-1ms。太爽了！！！ 边守岁边测试，看到这满意的结果，能安心的睡个好觉了。 越来越中意ruby了]]></description>
			<content:encoded><![CDATA[<p>早听说debian/ubuntu的1.8.6是debug模式，比正常的慢50%，一直没在意，反正是测试服务器不缺这点时间。</p>
<p>前几篇日志提到的600个节点的树递归生成时间与render耗时太长，要近200ms，c++render虽然快但不实用，于是今重写了扩展对erb进行yield，但最终只能优化到70ms(3ms树生成+70ms render)，最后发现即使什么都不作进行600次link_to操作也要花掉60-80ms。随抱着试试的心态升到了1.8.7<br />
升级后发现目录也改成像freebsd的/usr/local/&#8230;的样子不太熟悉。然后安装gem又折腾了一阵。最后怀着激动的心情安装好一试，！只要20-40ms！又试了试其它页面，原本要花0-6ms的页面全都稳定得变为0-1ms。太爽了！！！</p>
<p>边守岁边测试，看到这满意的结果，能安心的睡个好觉了。</p>
<p>越来越中意ruby了</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/326.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项实在耗时，鱼和熊掌不能兼得，得另想法子。
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/295.html/feed</wfw:commentRss>
		<slash:comments>2</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 #define _TEST_TEST_H extern int add(int,int); #endif //test.cxx #include "test.h" int add(int [...]]]></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 />
相关文件：</p>
<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></p>
<pre>%module test
%{
//包含头文件
#include "test.h"
%}
//接口add
int add(int,int);</pre>
<p><strong>2、编写wrap文件</strong></p>
<pre>swig -c++ -ruby test.i</pre>
<p>得到test封装文件 test_wrap.cxx</p>
<p><strong>3、编写test.h与test.cxx</strong></p>
<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></p>
<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></p>
<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></p>
<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>1</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[什么是Haml Haml，一款简洁的的类似Rhtml的插件，可以极大的简少写xhtml等视图代码的量。 安装haml 安装gem sudo gem install haml 安装haml plugin至rails haml &#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/s haml 154.83 reqs/s 下降约 2.45% 单个文件请求时间绝对值增加 0.159ms 对性能的影响还是微乎其微的 相关链接 haml首页 Haml [...]]]></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></p>
<pre>
%h1 Chapters#index
%p
  == Time: #{Time.now}
  =h "a~!@\#$%^&#038;*()+-"
</pre>
<p>输出</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>
