从class_eval'ed方法返回数组时的奇怪行为

时间:2011-09-30 15:08:14

标签: ruby-on-rails ruby activerecord

使用Ruby 1.9.2,我正在使用class_eval扩展一个类。

def slugged(fields)
  # assign string to variable only for easier debugging
  method = <<-EOS
    def slug_fields
      #{ fields.is_a?(Array) ? fields.inspect : ":#{ fields }" }
    end
  EOS

  class_eval method
end

只要字段是符号(例如slugged :name之后,slug_fields返回:name),此方法就可以正常工作。

但是,使用数组调用slugged会使slug_fields返回nil(例如slugged [:kicker, :headline]后,slug_fields会返回nil)。

奇怪的是,在调试slugged时,包含要创建的方法的字符串看起来与您期望的方式完全相同:

"          def slug_fields\n            [:kicker, :headline]\n          end\n"
"          def slug_fields\n            :name\n          end\n"

编辑:根据要求,为我打破的更完整版本:

module Extensions
  module Slugged
    extend ActiveSupport::Concern

    included do
      before_validation { |record| record.slug ||= record.sluggerize }
    end 

    module ClassMethods 

      def slugged(fields)
        # assign string to variable only for easier debugging
        method = <<-EOS
          def slug_fields
            #{ fields.is_a?(Array) ? fields.inspect : ":#{ fields }" }
          end
        EOS

        class_eval method
      end
    end

    module InstanceMethods
      def sluggerize
        fields = slug_fields

        slug_string = case
          when fields.is_a?(Array)
            fields.map { |f| self.send(f) }.join('-')
          else
            self.send fields
          end

        slug_string.parameterize
      end
    end
  end
end

class Article < ActiveRecord::Base
  include Extensions::Slugged  
  slugged [:kicker, :headline]
end

class Station < ActiveRecord::Base
  include Extensions::Slugged
  slugged :name
end

a = Article.new :headline => "this is a great headline!", :kicker => "attention-drawing kicker"
a.save # works, slug is set

s = Station.new :name => "Great Music"
s.save # TypeError: nil is not a symbol (in sluggerize where "self.send fields" is called)

1 个答案:

答案 0 :(得分:1)

您的代码在1.9.2下适用于我:

class Foo
  class << self
    def slugged(fields)
      method = <<-EOS
        def slug_fields
          #{ fields.is_a?(Array) ? fields.inspect : ":#{ fields }" }
        end
      EOS
      class_eval method
    end
  end
end

Foo.slugged :a
p Foo.new.slug_fields
#=> :a

Foo.slugged [:a,:b]
p Foo.new.slug_fields
#=> [:a, :b]

p RUBY_DESCRIPTION
#=> "ruby 1.9.2p180 (2011-02-18) [i386-mingw32]"

请您提供一个完整的,可运行的,独立的测试用例,为您打破?