What Does "Wrong Number of Arguments (1 for 0)" Mean in Ruby

What does wrong number of arguments (1 for 0) mean in Ruby?

When you define a function, you also define what info (arguments) that function needs to work. If it is designed to work without any additional info, and you pass it some, you are going to get that error.

Example:
Takes no arguments:

def dog
end

Takes arguments:

def cat(name)
end

When you call these, you need to call them with the arguments you defined.

dog                  #works fine
cat("Fluffy") #works fine


dog("Fido") #Returns ArgumentError (1 for 0)
cat #Returns ArgumentError (0 for 1)

Check out the Ruby Koans to learn all this.

Ruby Error: wrong number of arguments (1 for 0) (ArgumentError)

Look closely at which line number the ArgumentError is raised from (in your case, line 25).

You are passing one argument to the Text#initialize, but have not defined a version of initialize that takes one argument.

Try this instead (calling the default, zero argument Text constructor):

class Text
def post(success, error)
if authenticate?(@user, @password)
success.call
else
error.call
end
end
end
text = Text.new
success = ->{ puts "Sent!"}
error = ->{ raise 'Auth error'}
text.post(success, error)

Or define initialize with one argument:

class Text
def initialize(your_meaningful_argument)
# do stuff
end

def post(success, error)
if authenticate?(@user, @password)
success.call
else
error.call
end
end
end

How to fix ArgumentError: wrong number of arguments (given 0, expected 1) - RoR

I faced this issue while upgrading from Rails v5.2.3 to v5.2.4 and fixed it by upgrading the following gem

gem 'awesome_nested_set', '~> 3.2.0'

Wrong number of arguments (given 0, expected 1+) Rails

You need to change password_path for password_path(resource_name)

ROR: wrong number of arguments (given 2, expected 1)

The activeadmin gem instantiates the ActiveAdmin::OrderClause class with a couple of arguments (active_admin_config and order) as you can see here and here. Fix the initialize method arguments.

def initialize(active_admin_config, clause)
# ...
end

You should also remove the active_admin_config argument from the to_sql method since it is called with no arguments. You can set @active_admin_config in the initialize method and add :active_admin_config to the attr_reader call to use in the to_sql method.

module ActiveAdmin
class OrderClause
attr_reader :field, :order, :active_admin_config

def initialize(active_admin_config, clause)
@active_admin_config = active_admin_config
# rest of the code
end

def to_sql
# ...
end
end
end

I would recommend you create a CustomOrderClause class that inherits from the gem's ActiveAdmin::OrderClause class and only override the necessary methods. You can then use config.order_clause = CustomOrderClause when configuring activeadmin in an initializer.

rails syntax error: wrong number of arguments

You can use ruby_parser to eliminate your doubts about how this is actually parsed.

RubyParser.for_current_ruby.parse '(first_name +" "+ last_name)' results in:

s(:call, nil,
:first_name,
s(:call, s(:call, s(:str, " "),
:+@),
:+,
s(:call, nil,
:last_name)))

Effectively, you're getting first_name((+" ") + last_name), or in a more redundant/methody fashion, self.first_name((" ".+@).+(self.last_name)).

Meaning there is an argument in a call of the first_name method, stemming from accidental use of +@, the unary plus, on " ". But since first_name is an attribute getter (I guess?) and does not accept any arguments, you're getting an ArgumentError.


To help you interpret this output, a :call S-expression consists of:

  • :call
  • subject (a value on which a method is being called, nil if self)
  • method name as a symbol
  • 0 or more arguments

And by the way, Rubocop could have warned you about this:

Lint/AmbiguousOperator: Ambiguous positive number operator. Parenthesize the method
arguments if it's surely a positive number operator, or add a whitespace to the
right of the + if it should be a addition.
(first_name +" "+ last_name)
^

Consider adding it into your workflow.

wrong number of arguments (given 1, expected 0) error shown

Another way to write this

User.where('status <> 3').order(:firstname, :lastname).pluck(:name, :id)

The only difference is that the id won't be converted to a string so you can just append .map { |res| res[1] = res[1].to_s; res } if you really need this.



Related Topics



Leave a reply



Submit