twilio / twilio-ruby

A Ruby gem for communicating with the Twilio API and generating TwiML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

After updating Ruby version I get ArgumentError (wrong number of arguments (given 1, expected 0))

codeundercoverdev opened this issue · comments

Issue Summary

I was using ruby 2.6.0 and upgraded to Ruby 3.1.3 and now code that has been working for 3 years has broken. I am simply creating an sms message, but I am getting an error. I was using the twilio-ruby gem 5.30.0 but upgraded to 5.74.2 one I found this upgrade. I get the same error with both versions

Steps to Reproduce

  1. Look at the code snippet. Simply running the code produces this error: ArgumentError (wrong number of arguments (given 1, expected 0))

Code Snippet

	def send_pin_through_twilio
    	        return if Rails.env.development? || Rails.env.test?
	        client = Twilio::REST::Client.new
		client.messages.create({
			from: ENV["twilio_phone_number"],
			to: self.phone_number,
			body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
		})
	end

Exception/Log

2023-02-03T17:11:25.312087+00:00 app[web.2]: F, [2023-02-03T17:11:25.311995 #4] FATAL -- : [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312089+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] ArgumentError (wrong number of arguments (given 1, expected 0)):
2023-02-03T17:11:25.312089+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312090+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/models/user.rb:157:in `send_pin_through_twilio'
2023-02-03T17:11:25.312090+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/controllers/api/v3/users_controller.rb:127:in `login_next'
2023-02-03T17:11:25.320370+00:00 heroku[router]: at=info method=PUT path="/api/v3/users/login_next" host=app.myapp.co request_id=0d0b1732-1dbc-4170-a3d3-ba99bb7a676e fwd="73.179.143.247,108.162.212.48" dyno=web.2 connect=0ms service=2790ms status=500 bytes=307 protocol=https

Technical details:

  • twilio-ruby version: 5.74.2
  • ruby version:3.1.3

Change this:

client.messages.create({
  from: ENV["twilio_phone_number"],
  to: self.phone_number,
  body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
})

To this:

client.messages.create(
  from: ENV["twilio_phone_number"],
  to: self.phone_number,
  body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
)

Note that the second example is not a hash, it is using keyword arguments.

This is caused by the separation of positional and keyword arguments in Ruby 3.

Thanks for the comment @anothermh!
@codeundercoverdev Did using the keyword arguments resolve the errors you were seeing?

Closing due to inactivity. Please re-open this issue or open a new GitHub issue if you still need help.