显示标签为“Ruby”的博文。显示所有博文
显示标签为“Ruby”的博文。显示所有博文

2009年6月30日星期二

irb auto complete

在~/.irbrc中加入:

require 'irb/completion'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]

2009年3月29日星期日

fastri

sudo gem install fastri
fastri-server -b


然后就可以

qri string

如果显示大于一屏的话

qri string|less -r

2008年10月23日星期四

How To Generate Local RDocs

最近 http://www.ruby-doc.org/core/ 无法访问了,而 ri 用起来始终不习惯。偶然看到了个生成 Local RDocs 的方法。

http://www.mindsifter.com/2008/1/8/generate-local-ruby-core-rdocs
http://weblog.jamisbuck.org/2005/4/8/rdoc-template

Download the template here
To use it, copy it into the “rdoc/generators/template/html” directory of your Ruby installation.
Then:

rdoc --template=jamis --op <some output dir>


可能由于我机器上 rubygem 版本的原因,这个模板用不了,不过可以用自带的模板

rdoc --template=html --op <some output dir>


注:执行这个命令时,要先转到 ruby 源代码的目录,默认是生成该目录下所有文件的 rdoc。
也可以指定文件:

rdoc --template=html --op <some output dir> array.c

2008年6月19日星期四

FileUtils

FileUtils 是 ruby 的一个标准库。所提供的部分功能 核心库的 File 类也提供了,但更为强大和方便。
比如删除文件操作

File.delete(file_name)
FileUtils.rm(list, options)

强制删除

FileUtils.rm('NotExistFile', :force => true) # never raises exception

如果是File.delete的话,恐怕还得先判断一下:

File.delete('file_name') if File.exist?(file_name)

2008年6月16日星期一

const missing

用const_missing , const_set , const_get 来自动生成常量。
这三个方法都属于Module类的方法。

示例代码如下。其中Permission是个Model类。
module Perm
require "permission"
# lazy load consts
def self.const_missing(name)
if @perms.nil?
@perms = Permission.find(:all).map{|p|p.name}
@perms.each do |perm|
Perm.const_set( perm.gsub(/\./,'_').upcase , perm)
end
end
return nil if !const_defined?(name)
Perm.const_get(name)
end
end

enum 对应 symbol 类型

MySQL数据库字段是enum类型的话,对应的activerecorder对象属性是Symbol类型的。

2008年6月15日星期日

三元表达式 VS &&

无废话,下面两种写法是等价的:
self.user_id = new_user.nil? ? nil : new_user.id
self.user_id = new_user && new_user.id

2008年6月11日星期三

用mocha模拟多次调用同一个方法

标题起得比较绕口,看代码,无废话:
argu = [nil, nil, nil, nil, nil, nil, "s", nil, nil, nil, nil]
Something.expects(:a_method).times(argu.size).returns(*argu)
Something的a_method方法会被调用argu.size次,每次分别返回值是nil, nil, ..., "s", nil, ...

2008年6月10日星期二

生成\删除文件

Ruby中,生成一个文件:
Kernel::open(file_name, 'w')
删除一个文件:
File.unlink(file_name)
or
File.delete(file_name)

2008年6月7日星期六

instance_variable_set

instance_variable_set("@asset_group_#{type.to_s.downcase}", asset_group)
instance_variable_set是Object的方法,ruby核心库提供的功能。
意思是生成一个实例变量,变量的名字是由参数传进来的。

2008年6月5日星期四

在子类中调用父类的非同名方法

class A
def a
puts "base a"
end
end

class B < A
def b
puts "sub b"
# 调用A的方法a
A.instance_method('a').bind(self).call
end
end
http://rc.org.cn/forums/viewthread.php?tid=879
感谢汉东(blackanger

2008年6月4日星期三

学ruby要从娃娃抓起(转中转)





http://msdn.javaeye.com/blog/108220
因为引用的链接里也标明是转的,所以叫转中转。
我当年看的第一本Ruby相关的书就是这本啊。当时对Ruby根本没有一点概念,更加没有听说过Rails,去书店找了半天,一本Ruby的书都没找到,然后去前台查询,只查到了这本书(因为只有这本书的书名带了Ruby,其他的都只带了Rails),然后书店管理员又去找了半天,在Java分类里找到了。当时就觉得很寒。

2008年6月3日星期二

Gem::Specification

要把项目打成gem包的话,需要在Rakefile.rb里加些东西:
spec = Gem::Specification.new do |s|
s.name="bvi_lib"
s.version='0.1'
s.summary = 'A Ruby libary for Batch Video Ingestion'
s.email = 'bdong@freewheel.tv'
s.require_path = 'lib'
s.autorequire = 'bvi_lib'

s.executables << 'bvi'
s.files = FileList["{lib,doc,bin}/**/**"].to_a
# s.has_rdoc = true
s.author = "Bin Dong"
# s.extra_rdoc_files = ["README"]
# s.rdoc_options = ["doc"]
s.add_dependency("activesupport", '~> 2.0.2')
s.add_dependency("actionmailer", '~> 2.0.2')
s.add_dependency("activeresource", '~> 2.0.2')
s.add_dependency("xml-simple", '~> 1.0.11')
s.add_dependency("fastercsv", '~> 1.2.3')
end
(提醒:是'xml-simple'和'fastercsv',而不是'xmlsimple'和'faster_csv'。前面是gem包的名字,后面是程序中require用的。其实两个弄成一样的多好啊。)

注意add_dependency方法,这个是添加包的依赖关系,第一个参数是依赖的gem包名,第二个参数是版本关系,具体见Programming Ruby Second Edition, P206, Table 17.1
这里只摘'~>'的说明:

“Boxed” version operator. Version must be greater than or equal to the specified version and less than the specified version after having its minor version number increased by one. This is to avoid API incompatibilities between minor version releases.

minor就是中间的那个版本号。如果版本号是2.01,则三个部分分别是major = 2, minor = 0, tiny = 1

要打gem包的话,直接在项目下运行:

rake gem

就可以了。

但是注意,只修改Rakefile.rb文件的话,是不会重新打包的,必须把原先的gem包删掉,再重新打包才行。
因为打包的时候会自动检查
FileList["{lib,doc,bin}/**/**"]
下的文件有没有被修改,如果修改了,则会重新打包,如果没修改,就不会重新打包。

Rakefile.rb里的内容,在打包的时候,是放在gem包的metadata里的,查看metadata,就能知道Rakefile.rb文件修改后,重新打包是否成功。

查gem的命令:

gem list
gem list|grep xml
gem list|grep csv

2008年6月2日星期一

cattr_accessor not found

I installed activeresouce using RubyGems. And I want to launch the tests:

ruby /var/lib/gems/1.8/gems/activeresource-2.0.2-/test/format_test.rb

But it didn't work, I've got an error:

./../../lib/active_resource/base.rb:150: undefined method `cattr_accessor' for ActiveResource::Base:Class (NoMethodError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from ./../../lib/active_resource.rb:38
from ./../abstract_unit.rb:4:in `require'
from ./../abstract_unit.rb:4
from load_test.rb:1:in `require'
from load_test.rb:1

So I googled it, and found this topic
http://www.ruby-forum.com/topic/59288
Before starting tests, run this command:

export RUBYOPT=-rubygems

And then:

ruby /var/lib/gems/1.8/gems/activeresource-2.0.2-/test/format_test.rb

Haha, it works, but I don't know how it works. :(

2008年4月14日星期一

Install RMagick

  1. grab the ImageMagick.tar.gz:
    wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
  2. Unarchive it:
    tar xvzf ImageMagick.tar.gz
  3. install ImageMagick
    cd ImageMagick-6.3.8 "Or whichever the current version is, of course."
    ./configure
    make (wait~~~)
    sudo make install
  4. download rmagick-2.2.2.gem:
    wget http://files.rubyforge.mmmultiworks.com/rmagick/rmagick-2.2.2.gem
  5. install rmagick:
    sudo gem install rmagick-2.2.2.gem
  6. (see http://rmagick.rubyforge.org/install-faq.html, search "libMagickWand.so.1"):
    add this line "export LD_LIBRARY_PATH=/usr/local/lib" to ~/.bashrc
  7. test
    ruby -rubygems -e "require 'RMagick'; puts Magick::Long_version;"
    irb -rubygems -r RMagick

ref: http://zzhang.javaeye.com/blog/162994

2008年1月16日星期三

Mysql Ruby String 大小写排序陷阱(转)

以前一位同事的总结。

mysql 中: F > a
Ruby 中: a > F

说明: Mysql在字符串排序中, 大小写不敏感。
要小心在测试排序的时候, 注意这点。 可能会引起排序的test失败。

2008年1月13日星期日

2008年1月8日星期二

Teaching Rails at a University

RailsConf Europe 2007 上的一篇演讲。

开篇就振聋发聩:If Ruby is so great, why are Universities still teaching Java?

后面先是分析现实状况,谁适合来教(当然是演讲者),大学里的环境适不适合开这门课?似乎是不适合,在德国(演讲者是德国人,看那些ü啊ä啊就猜到了)大 学是做研究的,是科学领域的,培养爱因斯坦的。另一些学校(可能类似于国内的职业技术学院)是学技能(skills)的,修机器的。而学习编程语言是不属于科学领域的。

很遗憾,我承认计算机有一部分领域是需要理论的科学的研究的,但是大部分学生毕业了都是以敲键盘为生。编程是纯粹实践性的,看书听课做题考试这一套用处不大。

后面部分就是比较详细的讨论课程的计划,不但教Ruby和Rails,还教Agile和TDD。前两周还有个魔鬼训练课程(演讲者称其为blackout),"Lock up the students in a computer room for two weeks",每天都教好几样东西,拼命地灌,填,塞。

最后还有总结,看来是已经实践过一次了。哪些地方做得好,学生喜欢(学生们很喜欢那个两周的blackout),哪些地方做得不够,需要改进。

国外在学习推广新技术方面,从起跑线上就已经遥遥领先了。