在擴展到陣列類(軌道/的ActiveSupport/LIB/active_support/core_ext /陣列/ access.rb)以下函數定義:Ruby on Rails的 - 的ActiveSupport:數組擴展
# Returns the beginning of the array up to +position+.
def to(position)
if position >= 0
take position + 1
else
self[0..position]
end
end
爲什麼它是這樣定義的?爲什麼我們不能只是做:
def to(position)
self[0..position]
end
https://github.com/rails/rails/commit/ed03d4eaa89a7b4ab09e7f5da76b522d04650daf – bjhaid