I am having a problem making a change with an array in Ruby. The method is not able to use the global method. I am confused here and would love some assistance. How can I get an answer from a user and also use that answer?
我有一个问题,用Ruby中的数组进行更改。该方法不能使用全局方法。我在这里很困惑,我想要一些帮助。如何从用户那里得到答案,并使用这个答案?
a = [1,2,3,4,5]
puts "Pick number to insert into array:"
number = gets.chomp()
def insert(answer, number)
a.insert(1,number)
end
insert(answer,number)
Of course I am getting the error:
当然,我得到了一个错误:
methods.rb:13:in `insert': undefined local variable or method `a' for main:Object (NameError)
from methods.rb:16:in `<main>'
4 个解决方案
#1
5
The variable a
is not defined within the scope of the insert
method.
变量a不在insert方法的范围内定义。
There are several types of variables in ruby, which can be summarised by the following table:
ruby中有几种类型的变量,如下表所示:
$ A global variable
@ An instance variable
[a-z] or _ A local variable
[A-Z] A constant
@@ A class variable
In order for your code to work, you could either define a
as a global variable (although this is generally considered bad practice!):
为了让代码正常工作,您可以将a定义为全局变量(尽管这通常被认为是不好的做法!)
$a = [1,2,3,4,5]
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(answer)
$a.insert(1, answer)
end
insert(answer)
Or, you could define a
within the scope of the method:
或者,您可以在方法的范围内定义a:
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(answer)
a = [1,2,3,4,5]
a.insert(1, answer)
end
insert(answer)
Or, you could pass a
into the method as a parameter:
或者将a作为参数传递到方法中:
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(a, answer)
a.insert(1, answer)
end
a = [1,2,3,4,5]
insert(a, answer)
Or, you could define a class and make a
an instance variable of that class - for example, something like:
或者,您可以定义一个类并创建该类的实例变量——例如:
class HelloWorld
attr_reader :a
def initialize
@a = [1,2,3,4,5]
end
def insert(answer)
@a.insert(1, answer)
end
end
puts "Pick number to insert into array:"
answer = gets.chomp()
my_awesome_object = HelloWorld.new
my_awesome_object.insert(answer)
puts my_awesome_object.a
#2
0
Method declaration introduces the new scope. There are many ways to accomplish a task, e. g. with instance variable:
方法声明引入了新的范围。完成一项任务的方法有很多,例如使用实例变量:
@a = [1,2,3,4,5]
puts "Pick number to insert into array:"
number = gets.chomp
def insert(answer, number)
@a.insert(1,number)
end
#3
0
the method insert
dont have variable a
. You must send it there. So modify the code like this.
方法插入没有变量a,你必须把它发送到那里。像这样修改代码。
def insert(a, number)
a.insert(1,number)
end
insert(a, number)
I hope this helps
我希望这有助于
#4
0
Unlike blocks, methods do not capture the environment they were created in for the most part, other than object scope if a method is defined inside a class.
与块不同的是,方法在大多数情况下不会捕获它们所创建的环境,除非是在类中定义方法的对象范围。
In your case, passing a
explicitly to insert
could help:
在您的例子中,传递一个显式的插入可以帮助:
def insert(a, answer, number)
a.insert(1, number)
end
If a
and insert
go hand-in-hand, a good OO design might be to encapsulate that data and behavior into a separate class. Not sure if it's the case here, but something to keep in mind.
如果a和insert同时进行,一个好的OO设计可能是将数据和行为封装到一个单独的类中。不知道这里是不是这样,但要记住一点。
#1
5
The variable a
is not defined within the scope of the insert
method.
变量a不在insert方法的范围内定义。
There are several types of variables in ruby, which can be summarised by the following table:
ruby中有几种类型的变量,如下表所示:
$ A global variable
@ An instance variable
[a-z] or _ A local variable
[A-Z] A constant
@@ A class variable
In order for your code to work, you could either define a
as a global variable (although this is generally considered bad practice!):
为了让代码正常工作,您可以将a定义为全局变量(尽管这通常被认为是不好的做法!)
$a = [1,2,3,4,5]
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(answer)
$a.insert(1, answer)
end
insert(answer)
Or, you could define a
within the scope of the method:
或者,您可以在方法的范围内定义a:
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(answer)
a = [1,2,3,4,5]
a.insert(1, answer)
end
insert(answer)
Or, you could pass a
into the method as a parameter:
或者将a作为参数传递到方法中:
puts "Pick number to insert into array:"
answer = gets.chomp()
def insert(a, answer)
a.insert(1, answer)
end
a = [1,2,3,4,5]
insert(a, answer)
Or, you could define a class and make a
an instance variable of that class - for example, something like:
或者,您可以定义一个类并创建该类的实例变量——例如:
class HelloWorld
attr_reader :a
def initialize
@a = [1,2,3,4,5]
end
def insert(answer)
@a.insert(1, answer)
end
end
puts "Pick number to insert into array:"
answer = gets.chomp()
my_awesome_object = HelloWorld.new
my_awesome_object.insert(answer)
puts my_awesome_object.a
#2
0
Method declaration introduces the new scope. There are many ways to accomplish a task, e. g. with instance variable:
方法声明引入了新的范围。完成一项任务的方法有很多,例如使用实例变量:
@a = [1,2,3,4,5]
puts "Pick number to insert into array:"
number = gets.chomp
def insert(answer, number)
@a.insert(1,number)
end
#3
0
the method insert
dont have variable a
. You must send it there. So modify the code like this.
方法插入没有变量a,你必须把它发送到那里。像这样修改代码。
def insert(a, number)
a.insert(1,number)
end
insert(a, number)
I hope this helps
我希望这有助于
#4
0
Unlike blocks, methods do not capture the environment they were created in for the most part, other than object scope if a method is defined inside a class.
与块不同的是,方法在大多数情况下不会捕获它们所创建的环境,除非是在类中定义方法的对象范围。
In your case, passing a
explicitly to insert
could help:
在您的例子中,传递一个显式的插入可以帮助:
def insert(a, answer, number)
a.insert(1, number)
end
If a
and insert
go hand-in-hand, a good OO design might be to encapsulate that data and behavior into a separate class. Not sure if it's the case here, but something to keep in mind.
如果a和insert同时进行,一个好的OO设计可能是将数据和行为封装到一个单独的类中。不知道这里是不是这样,但要记住一点。