[英]wrong number of arguments (given 1, expected 2) (ArgumentError)
My goal is for a user to enter their screen resolution and store it in screen_size
. 我的目标是让用户输入屏幕分辨率并将其存储在screen_size
。 I did this, but I have an error. 我这样做了,但是我有一个错误。
class Screen
attr_accessor :screen_size, :info
def initialize(screen_size, info)
@screen_size = screen_size
@info = info
end
end
my_info = Screen.new("What is you're resolution?")
my_screen = Screen.new(gets.chomp)
# >> in `initialize': wrong number of arguments (given 1, expected 2) (ArgumentError)
How can I solve this? 我该如何解决?
You are passing wrong arguments to Screen.new
. 您将错误的参数传递给Screen.new
。 Your initialize
method expects two arguments and you are passing only one. 您的initialize
方法需要两个参数,而您只传递了一个。
Try this: 尝试这个:
screen = Screen.new(gets.chomp, "What is you're resolution?")
gets.chomp
returns one value (which is whatever you typed in before pressing enter). gets.chomp
返回一个值(这是您在按Enter之前键入的任何值)。 That method expects two. 该方法需要两个。 If you're entering something like "1024 768", then you could split the input by the space. 如果输入的是“ 1024 768”之类的内容,则可以按空格分隔输入内容。 Something like this: 像这样:
w, h = gets.chomp.split(' ')
my_screen = Screen.new(w, h)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.