• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

ActiveRecordを読み込んだ状態でirbを使っているときに、TAB補完をしようとすると落ちることが結構な頻度でありました。 原因を調べてみると、補完候補のArrayをsort使用としているところで、Symbolに対して <=> 演算子を呼びだそうとしてエラーが発生しているようです。 補完候補は Object#instance_methods から得ているようなので、なぜSymbolが要素として混入するのかが謎だったのですが、どうやら Arel::Attribute::Predications クラスが犯人のようです。

   1  PREDICATES = [
   2    :eq, :eq_any, :eq_all, :not_eq, :not_eq_any, :not_eq_all, :lt, :lt_any,
   3    :lt_all, :lteq, :lteq_any, :lteq_all, :gt, :gt_any, :gt_all, :gteq,
   4    :gteq_any, :gteq_all, :matches, :matches_any, :matches_all, :not_matches,
   5    :not_matches_any, :not_matches_all, :in, :in_any, :in_all, :not_in,
   6    :not_in_any, :not_in_all
   7  ]
   8  
   9  Predications = Class.new do
  10    def self.instance_methods *args
  11      warn "this module is deprecated, please use the PREDICATES constant"
  12      PREDICATES
  13    end
  14  end

このコードを書いた人は instance_methodsはStringの配列を返すという事を失念していたようですね。 irb/completeはObjectSpaceから補完候補を拾ってくるので、上述のコードがあるせいでSymbolが補完候補配列に混入してしまいます。

回避策としては、irb起動時に以下のようなコードを読み込むなどがあります。

   1  module Arel
   2    class Attribute
   3      class Predications
   4        def self.instance_methods
   5          require 'arel/algebra/attributes/attribute'
   6          Arel::Attribute::PREDICATES.map(&:to_s)
   7        end
   8      end
   9    end
  10  end

追記

この問題は 1.8.x系のRubyでのみ発生するようです。 thanks @a_matsuda!

posted by Png genki on Thu 7 Jul 2011 at 12:47

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1964
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ