对django的错失良机我一直觉得很遗憾。我去年大概10月的时候在对比了django和rails之后放弃前者选择后者作为项目开发框架。因为当时django的代码频繁改动(前几个月才完成大改,不过现在还不是1.0),ORM层API也很烦琐(后来按ActiveRecord风格重写了),没有整合的测试框架,文档相比Rails缺之甚多,python社团内部有人对django完全独立的一套full-stack系统有不同看法又搞了很多别的框架(比如turbogears);而当时Rails书出了一本,API已经稳定了很长一段时间,插件体系刚刚建立各种插件不断增加,DHH又‘碰巧’拉了几位AJAX高手进入核心 ...
robbin前面提到了一些局限,如遗留数据库是约定造成的,可以打破,但是以损失开发速度为代价。如工作流、规则引擎、报表是目前的技术限制,当然也可以通过java或.net桥用现成的。甚至自己通过socket或pipe去操纵外部程序,其实有些ruby的封装库就这么干的,不一定都通过动态链接库。
我再补充一些局限巴:
plugin缺乏依赖和命名冲突检查: plugin命名冲突随着plugin数量越来越多早晚会成问题,当然plugin开发员应该自觉用命名域包装。至于依赖,现在好像只有engine那几个有这个关系,不知道以后会怎样。其实依赖管理是现成的:Gems。把rails plugin包装成gem ...
Erb的渲染一直有人说慢,而c版本的eruby始终没有和Rails整合起来。终于一个东东横空出世了:Erubis, 宣称有Erb3倍的速度并和eruby比肩,而且还是纯ruby的。
安装很傻瓜:
1.安装gem
gem install erubis
2.然后在application.rb里加些整合代码(Erubis是独立于Rails的):
*放到enivronment.rb里也有效果的
require 'erubis/helper/rails'
suffix = 'erubis'
ActionView::Base.register_template_handler(su ...
context switch不足以表明coroutine,一般的方法调用(subroutine)也是context switch的,只有能保留上次context的(continuation)才是coroutine。Ruby的yield只是种匿名函数调用的syntax sugar。布娃娃用的yield 1, yield2, yield3的例子是用1,2,3去初始化块变量, block本身不记得上次的context:
>> def test2
>> yield
>> yield ...
I've just noticed that before_filter can absorb a block in order to
pass parameters to pre-executed actions. Here's an example:
before_filter{|c| c.authorized(1)}
However, in such case, action authorized must be public, while in the tradtional way,
before_filter :authorized
the action ...
*怎么转16进制?
class Integer
def to_hex
sprintf "%x", self
end
end
16.to_hex #=> 10
*怎么scan出utf-8字符串中的中文字符?
requrie 'jcode'
$KCODE = 'u'
class String
def scan_chinese
self.scan /[\340\270\200-\351\276\237]/
end
end
*怎么在case when里匹配一个array?
colors = %w{red green blue ...
如果你要定制Rails的违例输出页面的话的,一般会用这样的代码:
class ApplicationController < ActionController::Base
...
def rescue_action_in_public(exception)
case exception
when ::ActionController::UnknownAction
render_with_layout ....
when ActiveRecord::RecordNotFound
...
end
...
Rails提供三种页面cache方式:
action cache静态化action的结果但不会跳过filter,使用简单,麻烦最少,提速不多,一般够用。成批expire可以通过expire_fragment
fragment cache用来静态化页面的一部分。这种cache是非常基础的,被action cache在内部使用。默认使用文件系统做store,足够快,也可以改成memcache store。
完全把页面静态化的page cache能提速几十倍,效果极其明显。缺点也很明显:跳过任何filter, 无法控制访问权限。一个额外的好处是因为实际跳过了整个Rails,所以间接减少了FC ...
Ruby的irb和Unix shell一样,通过定制可以提供更多特性与方便。
配置:
1. 安装以下gem: wirble, map_by_method, what_methods
2. 建立一个名叫_irbrc文件,内容如下:
# Compiled by Cookoo
# Reference:
# http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb
# http://pablotron.org/software/wirble/
require 'rubygems'
require 'map_by ...
sql方式,会复制development数据库中的外键。相反,在schema模式下的外键是被完全忽略的,即使开发数据库里有,也不会复制到test数据库里。
而test:units执行的时候是根据environment.rb里的config.active_record.schema_format = sql设置(注释掉时为ruby dsl模式)来判断用哪种模式的。通过使用rake test:units --trace我们可以看见两者的差别:(忽略invoke,只看execute)
ruby dsl模式的schema下执行的task:
** Invoke test:units (first_t ...
如下代码,第一位正确说出它的功能的我会给4星评价,第一个发现其中有何逻辑bug并适当修改的我会给5星。Good luck~
class Proc
def ^ n
Proc.new do |*args|
(1..n).inject(args){|result, null| result = self.call(*result)}
end
end
end
I happen to read about a great analogue of so-called disruptive innovation on the current trend of ROR(Ruby on Rails, the emerging technology for easy web application development) at IDEA plugin forum. I'm also quite surprised at the fact that half of ROR users come from PHP world. Share with anyone ...
如果要一边声明一边初始化可以用这样的代码:
class Object
def better_accessor(attrs)
(class << self; self; end).send :attr_accessor, *attrs.keys
attrs.each {|k,v| instance_variable_set("@#{k}", v)}
end
end
然后就可以这样用:
class Test
def intialize
better_accessor :foo =>'bar' #初始化值即定死(值的正确 ...
布娃娃在另一个帖子提到很多差别,我觉得和那个主题不符,所以另外开一个贴讨论吧。
其实python和ruby非常接近,比大多数别的语言要接近的多,所以喜欢用啥就用啥(大实话,虽然也是废话)。语法上的差别虽然有那么一点,大部分是syntax sugar,我斗胆稍微列几个(python我也忘得差不多了,不对的大家尽管来鞭尸吧),但是主要差异还是设计思想上的:灵活vs明确. 我不认为两者在生产力上会有什么差别,如果你熟悉的话。*注意,仅限语言本身的比较。
1. ruby的case可以匹配很多东西:范围/数组,对象,正则表达,python没有case/switch而使用if/else比较死板点
2 ...
Ruby有不少惯用法,这里略作一些介绍,也方便阅读他人代码:
迭代
一般写法:
[code]
for i in (1..10)
puts i
end[/code]
习惯写法:
[code]
(1..10).each{|i| puts i}
或
1.upto(10){|i| puts i} # from njmzhang
[/code]
||=赋值
一般写法:
[code]
number = 1 if number.nil?
number = 1 unless number[/code]
习惯写法:
[code]
number ||= 1[/code]
程序入口 ...
对Rails开发方式我也在思考,对动态类型和meta programming已有的一些实践需要调整,也许需要引入一些新的做法。不得不说目前的大部分Rails项目都是少数几个人搞出来,即使那些访问量较大的成功站点不代表其代码量就大到需要很多人编程。所以对大规模的项目如何管理开发没有什么典型的例子。Getting Real涉及一些,不过更多是讲商业上的而不是软工上的实践。
动态语言不使用强制的interface而是用duck type,也就是隐性的接口。这种对象界面间的隐性接口在Rails里被进一步扩大,也就是robbin说的mvc之间的各种约定。显然在这种情况下测试被放在一个更重要的地位上,但 ...
所见即所得方式的问卷设计器终于搞定了,一些细节体会:
* 任何Ajax操作都应该加spinner(进度滚轮)和本地failure提示
* 注意id命名不要重复,但在使用observe_field时不同div中的元素可以重名
* Firebug是调试Ajax不二利器,配合web developer使用
* 不需要服务端处理的用link_to_function直接调用本地方法,比如cancel掉一个form
* 注意高亮各种直接show和hide的变化,否则改变太快让人难以察觉
* :update的用法:如果只更新一个元素就直接把要更新的容器设到:update里,如果更新多个元素否则设成""在服务 ...
Why不愧是一代Ruby大牛,连monadic combinator这个Haskell最著名的概念之一也能极简单地用Ruby复制出来:http://redhanded.hobix.com/inspect/hoppingThroughPipesAndClosures.html
突然感觉和java社团搞模式框架相比,ruby社团更津津乐道于这样的“奇巧淫技”,也许这正是文化差异和乐趣所在。
update: 前阵子被人推荐去学J语言,一种发源于古老的APL的面向数组/向量语言,适合数据分析。特点是简洁得过分,充满了one liner/line noise。虽然还是感觉非常难理解,不过确实有不少闪 ...
不经意间Vim对Rails的支持已经有飞跃性进步。Tim Pope从5月份开始发布了一个强大的Rails插件。 在文档的第一句他写道:TextMate may be the latest craze for developing Ruby on Rails applications, but Vim is forever.
Vim虽然有文件树,但是对Rails这样的文件夹用起来始终不太方便,现在这个插件提供了智能(哇)文件跳转。跳转命令是gf(别想歪了哈),会根据当前光标位置决定如何跳转。比如(*代表光标位置):
link_to "Home", :controller => :bl*og
...
Not to mention the recent two SD Jolts awards of Rails framework as well as its book as a strong proof of the increasing hotness, Rails is moving fast by releasing the version 1.1 RC1 after 3 months of the first offical version. Two of significant features among 500 changes are new RJS template whic ...
Happened to find this wonderful brain-washing presentation, especially helpful to (former) Java programmer like me. /lol(10) Learn Ruby Conventions(9) Everything is an Object(8) (Almost) Everything is a Message(7) Ruby is Way More Dynamic Than You Expect(6) Objects are Strongly Typed, Not Statically ...
I'm systematically learning Ruby during my lonely Christmas time... The grammar is way too flexible than I've ever seen in other languages. Definitely it's the language that delivers critical part of the power behind Rails. For anyone who is interested in, Try Ruby can be your first door to enter. T ...
Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite
A nicely written paper talking about the benefits and other concerns of switching from Java to ROR.
Happy programming time now.
- 浏览: 319012 次
- 性别:

- 来自: Montreal

- 详细资料
搜索本博客
我的相册
20059805856241
共 10 张
共 10 张
最新评论
-
Darcs简介
good 3x
-- by 夜鸣猪 -
Pratical Ocaml作者采访
现在主要用F#分析数据,因为比较舒服(人懒啊)。其实也只用到很少的FP特性,Ru ...
-- by cookoo -
Pratical Ocaml作者采访
一年多了,呵呵,cookoo能说说看,学习使用OCaml的进展和体会吗?
-- by billgui -
Memory - 柿岛伸次
还不错啊。
-- by hazzy -
Memory - 柿岛伸次
我很想下这个,可就是不能下。LZ能否提供链接
-- by yeshucheng






评论排行榜