看rails3源码的一些疑问
在nested_attributes.rb有一段没有理解Ruby代码existing_records = if association.loaded?
association.to_a
else
attribute_ids = attributes_collection.map { |a| a['id'] || a[:id] }.compact
attribute_ids.present? ? association.all(:conditions => {association.primary_key => attribute_ids}) : [ ]
end这里的为什么会有else情况存在,而且如果是else情况的我觉得整个代码就有问题了,得到的不是期望的结果。
同样这个文件里有REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |_, value| value.blank? } }
这里的下划线_是什么意思?
第三个问题就是active_support中有一个Concern的类,里面有个方法叫append_features的方法,这个方法是什么时候调用的? 本帖最后由 bugbugbug3 于 2011-01-06 16:03 编辑
Rails的源码没怎么看过。不过我可以说说的第二个关于下划线的问题。
在ruby中,下划线"_" 是标识符的合法组成部分。而且下划线"_"本身就是一个合法的标识符。
如:_ = "Hello,Ruby!"
p _一般来说,如果你不关心某个变量的名称,可以用"_" 。如:a = ["name","2011"]
_,year = a
p year在这里,我不关心name,只关心把数组a中的年份。
一句话! “_” 和 其他变量一样。 回复 2# bugbugbug3
3Q,非常3Q! 看activesupport里面的dependencies.rb这个文件
append_features就是简化了module在被include的时候哪些作为class method,哪些作为instance method的
页:
[1]