mysql,Linux,HighPerformance,ruby on Rails

2009年10月14日星期三

关于CustomValidates的使用demo,及一个ruby中的The splat operator.

CustomValidates,源自看advanced rails recipes里的recipe 10:
Write Your Own Custom Validations .
书中使用的rails版本已经很老,所以在rails 2.3下使用书中讲的过不了。
参考这里:
http://chrisblunt.com/blog/2009/04/18/rails-writing-dry-custom-validators/
把validates放在config/initializers里,不需要在environment.rb里加require就OK了。
直接按书中写test unit,直接通过。:-)
然后涉及到给class里加mothod的方法,看这里
http://blog.codevader.com/2008/09/27/including-and-extending-modules-in-ruby/
讲了一遍,我看了遍,记了一下。
然后又涉及到一个 *号,splat operator,
看这里
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
注意一下comments里的一个例子:
It may also help to note that ’splat’ returns a copy of the instance that it’s used with, whereas not using ’splat’ returns a reference to the instance.
说的就是加*的是返回一个实例的copy,不加*的是返回一个实例的reference,下面代码一目了然。

Ex.\
[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, purple
]

[
>group = ["blue", "green", "pink"]
>painting = ["paintbrush", "canvas", *group]
>group[2] = “purple”
>puts painting

gives us: paintbrush, canvas, blue, green, pink
]

没有评论:

发表评论