Techioz Blog

Ruby: ArgumentError (引数の数が間違っています (1 が指定され、0 が期待されます)) [クローズ]

概要

Railsで引数を渡してメソッドを呼び出す方法が根本的に理解できていないことは明らかです。

ダミー情報を使用してストライプへのテスト呼び出しを行おうとしていますが、コンソールから次のコマンドを実行しようとすると、件名エラーが発生します。

Stripe::CustomerApi.new(30).create_stripe_customer

メソッドが配置されている私のクラスは次のとおりです。

require 'stripe'
module Stripe
    class CustomerApi

        def initialize(id)
          @user = User.find(id)
          @company_profile = @user.company_profile
          Stripe.api_key = 'my test key info'
        end


        def create_stripe_customer
          request = Stripe::Customer.create({
              email: '[email protected]',
              name: 'john doe',
              address: {
                city: 'Brothers',
                country: 'US',
                line1: '27 Any Street',
                postal_code: '90210',
                state: 'CA',
              }
            })

          Rails.logger.info request

        end
    end
end

ここでの考え方は、テストが機能するようになったら、実際のユーザー情報を ID 値を通じて渡すということです。

Railsコンソールからスタックトレースを追加します。

irb(main):001:0> Stripe::CustomerApi.new(30).create_stripe_customer
  User Load (7.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 30], ["LIMIT", 1]]
  CompanyProfile Load (2.7ms)  SELECT  "company_profiles".* FROM "company_profiles" WHERE "company_profiles"."user_id" = $1 LIMIT $2  [["user_id", 30], ["LIMIT", 1]]
Traceback (most recent call last):
        3: from (irb):1
        2: from app/services/stripe/customer_api.rb:15:in `create_stripe_customer'
        1: from app/services/stripe/stripe_client.rb:11:in `initialize'
ArgumentError (wrong number of arguments (given 1, expected 0))

トレースは stripe_client ファイルを参照していますが、その理由はわかりません。これは、REST API を使用してこれと同じ API 呼び出しを試みた別のファイルです。同じメソッドはありません。

解決策

両方のファイルのフォルダー構造が異なり、両方のファイルの名前が異なると仮定しています。 フォルダー構造を使用してローカルマシンでコードを実行します アプリ/サービス/ストライプ/customer_api.rb

このコードは、以下で説明するコマンドを実行すると正常に動作します あなたも同じことを試してみるべきです

Stripe::CustomerApi.new(1).create_ Stripe_customer