【问题标题】:why is ruby expecting wrong number of arguments为什么 ruby​​ 期望错误数量的参数
【发布时间】:2021-03-04 10:16:47
【问题描述】:

我有一个 Rails 应用程序,我在其中设置方法 before_save activeRecord 回调,如下所示:

class GroupEvent < ApplicationRecord
    enum status: [ :published, :draft ]
    before_save :calculate_and_set_dates
    def calculate_and_set_dates
        missing_properties = []
           #check for available or set attributes 
           puts  "sss" + self.startDate.to_s
             if !self.startDate 
                   missing_properties<<"startDate" 
             end
             if !self.duration 
                missing_properties<<"duration" 
          end
           if !self.endDate
            missing_properties<<"endDate" 
            end
           
            binding.pry

           if  missing_properties.length<=1
             set_missing_property(missing_properties[0]) 
             else
                set_errors_for(missing_properties)
             end

  end

    private
    def set_missing_property(missing_property)
        case  missing_property
         when "startDate"
                 self.startDate = self.endDate - self.duration
         when "duration"
            self.duration = self.endDate -  self.startDate
         when  "endDate"
             self.endDate=  self.startDate +self.duration
        
       end
    end

结束

注意:这个注释是完整的类,所以不要担心 set_errors_for 方法的实现。

现在,我使用 GroupEvent.create(name:"hackaton",description:"hecking my life away",startDate: DateTime.now, duration:10) 创建一个 groupEvent。在调用 set_missing_property(missing_properties[0]) 时,我收到错误数量的参数错误:

ArgumentError: wrong number of arguments (given 1, expected 0) from /Users/haroonAzhar/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/attribute_methods/read.rb:15:in startDate'

如您所见,set_missing_property 方法在定义中有 1 个参数,为什么它期望为 0?更令人困惑的是:当我没有向“set_missing_property”方法传递任何参数时,我得到了这个错误:

ArgumentError: wrong number of arguments (given 0, expected 1) from /Users/haroonAzhar/Desktop/develop/whitespectre/app/models/group_event.rb:29:in set_missing_property'

我不知道它为什么在看

/Users/haroonAzhar/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/attribute_methods/read.rb:15:in startDate'

当我给出一个参数/参数但我检查了它所建议的文件时,它所指的部分看起来像这样:

module ClassMethods # :nodoc:
    private
      def define_method_attribute(name)
        ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
          generated_attribute_methods, name
        ) do |temp_method_name, attr_name_expr|
          generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{temp_method_name}
              name = #{attr_name_expr}
              _read_attribute(name) { |n| missing_attribute(n, caller) }
            end
          RUBY
        end
      end
  end

当定义的方法在类中时,甚至在那个地方寻找什么?但我需要回答的真正问题是,为什么它期望的参数数量错误,我该如何解决?

提前感谢您的帮助,非常感谢:D

【问题讨论】:

  • 对您的编码风格只有两个小改进。在 Ruby 社区中,通常使用下划线变量名(start_date 而不是 startDate)和 idents 代码用 2 而不是 4 空格。当然,您的代码仍然可以,但是当您将来想与团队中的其他 Ruby 开发人员一起工作时,我建议您开始遵循通用的 Ruby 样式指南,例如 Ruby Style Guide
  • 感谢您对样式指南的引用。我主要使用 js,所以我对 ruby​​ 有点陌生。

标签: ruby-on-rails ruby activerecord rubygems


【解决方案1】:

好吧,我解决了(也许)

所以在我的set_missing_property 内的endDate 案例中,我将其更改为:

when  "endDate"
         self.endDate =  self.startDate + self.duration

我相信我调用 self.startDate 时带有一个不期望有任何参数的参数,所以它抛出了那个错误。

感谢所有为此付出时间的窥视者。真的很感激。

【讨论】:

  • 空白很重要。 self.startDate +self.duration 中的 + 将被解释为一元运算符,因此添加括号后,Ruby 会将其视为self.startDate(+self.duration)。所以要小心并与你的空格保持一致(包括缩进)。
  • 为什么我回答这个问题没有获得声誉积分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
相关资源
最近更新 更多