【问题标题】:uninitialized method first_name using fixy gem使用 fixy gem 的未初始化方法 first_name
【发布时间】:2017-10-18 19:03:17
【问题描述】:

我正在使用这个 gem 来帮助我制作固定的文件格式化程序。我正在关注文档但收到错误

require('fixy')
class Record < Fixy::Record
  include Fixy::Formatter::Alphanumeric
  # Define record length
  set_record_length 20

  # Fields Declaration:
  # -----------------------------------------------------------
  #       name          size      Range             Format
  # ------------------------------------------------------------

  field :first_name,     10,     '1-10' ,      :alphanumeric
  field :last_name ,     10,     '11-20',      :alphanumeric

  # Any required data for the record can be
  # provided through the initializer

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name  = last_name
  end

  # 2) Using a method definition.
  #    This is most interesting when complex logic is involved.
  def last_name
    @last_name
  end
end

shinken =  Reky.new('Sarah', 'Smith')
p shinken.generate

最后一个方法:shinken.generate 给出错误:generate': undefined methodfirst_name' for # (NoMethodError)

这可能是什么原因造成的?

【问题讨论】:

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


    【解决方案1】:

    试试这个:

      def initialize(fn, ln)
        @first_name = fn
        @last_name  = ln
      end
    

    您的方法参数名称可能与实例变量发生冲突?

    【讨论】:

    • 实际上添加了一个类似于 def last_name 的方法,但使用 first_name 而不是 last name,已修复它。我不知道为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多