Rails3.0 released!

终于 Rails3.0千呼万唤始出来。

感觉其实每次Rails升级都会带来不小的变化。这次3.0的变化尤胜!

按照DHH的说法:

Rails 3.0 has been underway for a good two years, so it’s with immense pleasure that we can declare it’s finally here. We’ve brought the work of more than 1,600 contributors together to make everything better, faster, cleaner, and more beautiful.

Rails升级到2.2.2之后支持本地化的一个小BUG

家里的电脑上,Rails环境升级到了2.2.2。发现这个版本的Rails内置了i18n的支持。很开心,因为可以摆脱”1 day ago” 这种信息了(当然,之前可以通过安装别的插件或者Gem来解决,但是这次是内置的诶。)

决定在现在练手的项目上实践一把。找到”locales”的文件夹,里面有一个en.yml文件(dammy),通过文件中的提示,可以去下载其他语言的yml。把zh-CN.yml添加到 locales 文件夹之后,就是去enviroment.rb修改设置了。

有两行被注释掉的语句

# config.i18n.load_path << Dir[Rails.root.join(‘my’, ‘locales’, ‘*.{rb,yml}’)]
# config.i18n.default_locale = :de

解除这两句的注释,把默认的 :de 改成 :’zh-CN’  当然,这里要注意这个引号同学。信心满满地认为已经没有问题了。可是,一运行却发现报错,没法把时间转化成String。我蒙了,在Rails社区提问没有人理(估计是已经解决了这个问题?)只好自己在Google上找答案。终于让我发现了这个页面,似乎是说在config.i18n.load_path << Dir[Rails.root.join(‘my’, ‘locales’, ‘*.{rb,yml}’)]这句中的 <<符号应该换成 =号就对了。马上着手修改,发现在”show”的时候没有问题了,在”new”的时候继续失败…

很挫折,在看了上面提到页面的评论,发现新的enviroment.rb已经修正了这个问题,再对比新的enviroment.rb才发现<<符号应该改成 +=才对。

现在,已经可以正常地显示中文的时间了。

这个故事告诉我们: 要是我第一想到的是Google的话,解决这个问题就用不了2天时间了。等待是没有结果的,Just Do IT :-)

Ruby中Load的用法和一个疑问

在Ruby中读取一个文件有两种方法:require和load。这两者的区别在于:

load “file.rb”
load “file.rb”

这段代码会把file.rb读取两边。而如果是:

require “file.rb”
require “file.rb”

这样子的代码,file.rb只是在第一次的时候被载入,接下来那次其实是没有作用的。所以如果遇到一些程序的值是被修改过的,我们就需要用到load来保证ruby读到的是最新的内容。

可是我的问题出现在第二次用load读取文件的时候。我先建立一个文件名为reqdemo.rb的文件:

puts “This is the first (master) program file”
require ‘requiree.rb’
puts “And back again to the first file.”

先让ruby输出一段字符”This is the first (master) program file”,然后在读取reuiree.rb的文件内容,最后再输出一段字符”And back again to the first file.”。接着我们创建requiree.rb文件,输入如下代码:

puts “>This is the second file, which was ‘required’ by the first.”

所以如果运行reqdemo.rb,我们能得到如下结果:

c:\ruby4rails>reqdemo.rb
This is the first (master) program file.
>This is the second file, which was ‘required’ by the first.
And back again to the first file.

最后我们建立一个loaddemo.rb并输入如下代码:

load “reqdemo.rb”
load “reqdemo.rb”

把reqdemo.rb读取两遍。执行loaddemo.rb应该得到重复的两个reqdemo.rb的结果。可是现在我得到的结果是:

c:\ruby4rails>loaddemo.rb
This is the first (master) program file.
>This is the second file, which was ‘required’ by the first.
And back again to the first file
This is the first (master) program file.
And back again to the first file

在第二次load reqdemo.rb时,requiree.rb没有被读取…这是为什么?我到现在还没有想通…

Technorati Tags: , , , ,

Ruby学习笔记2

今天碰到的问题两个:
1.class/module name must be CONSTANT
这个问题发生在定义的类或者模块首字母没有大写,我刚开始的时候是这么写的:

irb(main):002:0> class song
irb(main):003:1> def name
irb(main):004:2> @name
irb(main):005:2> end
irb(main):006:1> def artist
irb(main):007:2> @artist
irb(main):008:2> end
irb(main):009:1> def duration
irb(main):010:2> @duration
irb(main):011:2> end
irb(main):012:1> end

于是在最后一个end的时候发生了如下错误:

SyntaxError: compile error
(irb):2: class/module name must be CONSTANT
from (irb):12
from:0

再修改了之后,输入:

irb(main):013:0> class Song
irb(main):014:1> def name
irb(main):015:2> @name
irb(main):016:2> end
irb(main):017:1> def artist
irb(main):018:2> @artist
irb(main):019:2> end
irb(main):020:1> def duration
irb(main):021:2> @duration
irb(main):022:2> end
irb(main):023:1> end
=> nil

在最后一个end时得到nil值,不过接下来就碰到第二个问题了.

2.ArgumentError: wrong number of arguments
这个错误是参数设置错误,通常后面会跟一个括号指出有几处错误..比如我这个:

irb(main):024:0> song=Song.new(“Bicylops”,”Fleck”,260)
ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize’
from (irb):24:in `new’
from (irb):24
from :0
irb(main):025:0> song.artist

不过我不明白我的错误到底发生在哪里,是初始化new这个方法时参数错误…?网上很难找到相关信息,有没有高手指点一下?

Technorati Tags: , ,

Ruby的instSection怎么用?

最近在看Programming Ruby英文的第二版。

因为工作很忙的关系,所以看书的进度一直很慢。到今天还在看Array和Hash的那个部分。

在看书中的例子的时候,发现”instSection”的用法我不是很明白。在我下载的PDF文档上写的是inst_section,可是我发现在网上查到的资料都是instSection -________-

另外,就算是instSection,我能够书面上理解例题的含义,可惜自己写的脚本却没法成功地验证..真是郁闷啊。


Technorati : , ,