I'm getting an error with the following example when running the program.
我在运行程序时遇到以下示例的错误。
The error reads as such:
错误如下:
burger.rb:8:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
from burger.rb:19:in `new'
from burger.rb:19:in `<main>'
Any help is appreciated for this noob, the code is below:
这个菜鸟的任何帮助表示赞赏,代码如下:
$toppings = false
class Burger
AVAILABLE_TOPPINGS = ["lettuce", "tomato", "onion", "cheese"]
attr_reader :options
def initialize
@toppings = []
end
def order
print "how many burgers would you like? "
number = gets.chomp
puts "#{number} burgers coming right up boss!"
end
end
burger = Burger.new("lettuce")
burger.order
3 个解决方案
#1
2
As others have said your initializer is expecting no arguments but you're giving it lettuce
. If you're using ruby 2.1 or later I would suggest using keyword arguments:
正如其他人所说的那样,你的初始化者不会有任何争论,但是你会给它生菜。如果您使用ruby 2.1或更高版本,我建议使用关键字参数:
class Burger
TOPPINGS = %i[lettuce tomato onion cheese]
attr_reader :toppings
def initialize(toppings: [])
@toppings = TOPPINGS & toppings
end
end
This allows you do to Burger.new(toppings: [:lettuce])
which I feel is a lot more readable.
这允许你做Burger.new(浇头:[:生菜]),我觉得它更具可读性。
#2
1
The error is telling you that the method initialize
expects 0 argument, while you give it 1 ("lettuce"
in Burger.new("lettuce")
).
错误告诉你方法initialize需要0参数,而你给它1(Burger.new(“生菜”)中的“生菜”)。
You need to make initialize
expecting one argument:
你需要初始化期望一个参数:
def initialize(options)
@toppings = []
@options = options
end
#3
0
$toppings = false
is code smell. Globals are generally not necessary, and should only be used when you're absolutely sure they're needed. When you're first learning an OO language I think it's better to avoid them and learn about variable scoping.
是代码味道。全局通常不是必需的,只有在您完全确定需要它们时才应使用。当你第一次学习OO语言时,我认为最好避免它们并学习变量范围。
In this case, you don't use it in your sample code, but you do use:
在这种情况下,您不要在示例代码中使用它,但您确实使用:
@toppings = []
(which is again not used elsewhere). It isn't a good idea to name a global variable the same as an instance variable because it's too easy to use one when you mean the other, and introduce a bug.
(这在其他地方也没有使用过)。将全局变量命名为与实例变量相同并不是一个好主意,因为当你的意思是另一个时,它太容易使用了一个,并引入了一个bug。
#1
2
As others have said your initializer is expecting no arguments but you're giving it lettuce
. If you're using ruby 2.1 or later I would suggest using keyword arguments:
正如其他人所说的那样,你的初始化者不会有任何争论,但是你会给它生菜。如果您使用ruby 2.1或更高版本,我建议使用关键字参数:
class Burger
TOPPINGS = %i[lettuce tomato onion cheese]
attr_reader :toppings
def initialize(toppings: [])
@toppings = TOPPINGS & toppings
end
end
This allows you do to Burger.new(toppings: [:lettuce])
which I feel is a lot more readable.
这允许你做Burger.new(浇头:[:生菜]),我觉得它更具可读性。
#2
1
The error is telling you that the method initialize
expects 0 argument, while you give it 1 ("lettuce"
in Burger.new("lettuce")
).
错误告诉你方法initialize需要0参数,而你给它1(Burger.new(“生菜”)中的“生菜”)。
You need to make initialize
expecting one argument:
你需要初始化期望一个参数:
def initialize(options)
@toppings = []
@options = options
end
#3
0
$toppings = false
is code smell. Globals are generally not necessary, and should only be used when you're absolutely sure they're needed. When you're first learning an OO language I think it's better to avoid them and learn about variable scoping.
是代码味道。全局通常不是必需的,只有在您完全确定需要它们时才应使用。当你第一次学习OO语言时,我认为最好避免它们并学习变量范围。
In this case, you don't use it in your sample code, but you do use:
在这种情况下,您不要在示例代码中使用它,但您确实使用:
@toppings = []
(which is again not used elsewhere). It isn't a good idea to name a global variable the same as an instance variable because it's too easy to use one when you mean the other, and introduce a bug.
(这在其他地方也没有使用过)。将全局变量命名为与实例变量相同并不是一个好主意,因为当你的意思是另一个时,它太容易使用了一个,并引入了一个bug。