你是 @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。
#21 楼 @scriptfans 我自己写的 plugin 是幂等的,如果原插件不幂等就加个 wrapper:
$(document).on 'page:change', ->
$('textarea[data-behaviors="editor"]').each ->
element = $(this)
if not element.data('editor')
element.data 'editor', element.editor()
提供几个模式:
1. 整站共享的逻辑,例如购物车:
$(document).on 'click', '.cart', ->
# code here
2. 特定元素存在的时候触发的逻辑,例如 editor:
$(document).on 'page:change', ->
$('textarea[data-behaviors="editor"]').editor() # a jquery plugin
3. 特定元素由用户触发的逻辑,例如 modal:
$(document).on 'click', '[data-toggle="modal"]', ->
# toggle modal
4. 某个页面特有的逻辑:
写到页面末尾,用内嵌形式。
<script>
// code here
</script>
现在不用操心 ready 了,page ready 是 jQuery 带来的思维方式,在 Turbolinks 环境需要另一种思维方式。无论最终是否用 Turbolnks,理解它,就会发现自己对浏览器如何执行 JavaScript 有更深的理解。
如果是前端开发者,应该比我更熟悉 Ember.js,Angular.js 这些框架,在这些框架生成的页面元素上要怎么操作 DOM 呢?Angular.js 的 FAQ 里面说,不要自己操作 DOM(Stop trying to use jQuery to modify the DOM in controllers.),为了打破习惯,还建议移除 jQuery(If you're struggling to break the habit, consider removing jQuery from your app.),这可不止要改变事件绑定的习惯。现在前端开发者可喜欢捣鼓新玩意了,我不担心他们。