新手问题 ruby 语法疑问

benzhang · 2012年09月05日 · 最后由 Shadow 回复于 2012年09月05日 · 1941 次阅读
module NestedForm
  module ViewHelper
    def nested_form_for(*args, &block)
      options = args.extract_options!.reverse_merge(:builder => NestedForm::Builder)
      form_for(*(args << options), &block) << after_nested_form_callbacks
    end

    if defined?(NestedForm::SimpleBuilder)
      def simple_nested_form_for(*args, &block)
        options = args.extract_options!.reverse_merge(:builder => NestedForm::SimpleBuilder)
        simple_form_for(*(args << options), &block) << after_nested_form_callbacks
      end
    end

    if defined?(NestedForm::FormtasticBuilder)
      def semantic_nested_form_for(*args, &block)
        options = args.extract_options!.reverse_merge(:builder => NestedForm::FormtasticBuilder)
        semantic_form_for(*(args << options), &block) << after_nested_form_callbacks
      end
    end

    def after_nested_form(association, &block)
      @associations ||= []
      @after_nested_form_callbacks ||= []
      unless @associations.include?(association)
        @associations << association
        @after_nested_form_callbacks << block
      end
    end

    private
      def after_nested_form_callbacks
        @after_nested_form_callbacks ||= []
        fields = []
        while callback = @after_nested_form_callbacks.shift
          fields << callback.call
        end
        fields.join(" ").html_safe
      end
  end
end

对于像这样的代码

simple_form_for(*(args << options), &block) << after_nested_form_callbacks

谁能解释一下 << 是什么意思啊? 我知道<<有字符串加的意思还有Singleton class definition. 但这里我看得不是太懂,请指教了. 谢谢

共收到 4 条回复

fields = [] 这里定义了数组 fields << callback.call 这个就是把某个东东丢到数组里咯

哦 我明白了 其实只是很简单的一个结果相加 被ruby的语法吓到了

查看ruby的规范

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册