通过Active Support Core Extensions guide,看起来其中一些不可用。以下是新会话的逐字输出:
$ rails --version
Rails 3.1.3
$ rails console
[1] pry(main)> [].prepend(10)
NoMethodError: undefined method `prepend' for []:Array
from (pry):1:in `<main>'
[2] pry(main)> [].unshift(10)
=> [10]
[3] pry(main)> %w(a b c d).append('e')
NoMethodError: undefined method `append' for ["a", "b", "c", "d"]:Array
from (pry):3:in `<main>'
[4] pry(main)> %w(a b c d) << 'e'
=> ["a", "b", "c", "d", "e"]
[5] pry(main)> require 'active_support/core_ext/array'
=> false
没有任何迹象表明append
和prepend
仅被弃用或仅限于尖端,所以这里发生了什么?
看起来这些实际上不属于Rails 3.1.3,因为它们失败了:
$ grep 'prepend\|append' ~/.rvm/gems/ruby-1.9.3-p0@project_name/gems/activesupport-3.1.3/lib/active_support/core_ext/array/*
$ ls ~/.rvm/gems/ruby-1.9.3-p0@taclom/gems/activesupport-3.1.3/lib/active_support/core_ext/array/prepend_and_append.rb