Rails 有 ActiveSupport::Notifications http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html
你是 @mogodb 马甲么?你能停止 Troll 行为么?
YAML
原文没有 location 这个配置块。
在每个配置块 passenger 配置会清空,所以我怀疑是加了这个配置块导致的问题。
还有个问题我还没想好,不知道还有谁碰到这个需求:支持使用多个商户帐号 https://github.com/chloerei/alipay/issues/27
Ruby 线程有 GIL,可能与这个有关。
system :python, 'path/to/script.py'
从基础说起,可以用一个 Hash 对应一个 Person,一个 Person 数组就是:
persons = [ { name: '李xx', sex: '', tel: '' }, { name: '张', sex: '', tel: '' } ] # and more
数组(Array)内含了不少方法,其中的 inject 方法可以将数组遍历然后计算出一个结果,文档看这里 http://ruby-doc.org/core-2.2.1/Enumerable.html#method-i-inject ,对应你的需求就是:
persons.inject(Hash.new(0)) {|hash, person| hash[person[:name][0]] += 1; hash }
# => { '李' => 1, '张 => 1 }
解释一下,Hash.new(0) 创建一个空 Hash 用来存放结果(这个 Hash 每个 key 的默认值是 0),然后传到 inject block 里面;block 里面的 person[:name][0]
取到用户的姓,然后作为 key 找到 hash 的对应值 += 1;最后 block 要将 hash 作为结果返回作为下一次迭代的 hash 值,遍历完毕后 hash 就是你要的结果。
如果你希望 Person 成为一个类,方法差不多,代码变为:
class Person
# attr_accessor 定义了 getter setter 方法
attr_accessor :name, :sex, :tel
def initialize(attributes)
@name = attributes[:name]
@sex = attributes[:sex]
@tel = attributes[:tel]
end
end
persons = [ Person.new( name: '李xx', sex: '', tel: ''), Person.new( name: '张xx', sex: '', tel: '') ]
persons.inject(Hash.new(0)) {|hash, person| hash[person.name[0]] += 1; hash }
以上是 Ruby 的做法,你问到了 Rails 的做法,那么假设数据是存在数据库里的,并且使用了 ActiveRecord,那么可以通过数据库查询得出:
class Person < ActiveRecord
end
# PostgreSQL 语法
Person.select('substring(name for 1) as first_name, count(*)').group('first_name').map{ |person| [person.first_name, person.count] }.to_hash
#10 楼 @flowerwrong 继承 railtie 可以实现以下目标:
http://api.rubyonrails.org/classes/Rails/Railtie.html
目前只利用了它自动添加 lib/rails 目录下的 generator 这个功能。
#3 楼 @davidminaki 因为不知道你的源码,只能问你有没有做过什么特别的事了,例如在 Rails 载入前执行了什么代码。
User 类继承了什么类?
这个绿对我有些刺眼。
我想不到理由不用 production。