数组#prepend / append在Rails 3.1.3中未定义?

时间:2012-02-01 09:56:21

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

通过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

没有任何迹象表明appendprepend仅被弃用或仅限于尖端,所以这里发生了什么?

看起来这些实际上不属于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

2 个答案:

答案 0 :(得分:4)

3.2.0中的这些方法were added,只不过是<<unshift的别名。

答案 1 :(得分:1)

看起来它只能从Rails v3.2.1获得。