Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 混淆实例变量和局部变量_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 混淆实例变量和局部变量

Ruby on rails 混淆实例变量和局部变量,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Ruby On Rails 3.2,我最近开始学习ruby,我对实例变量、局部变量和类变量感到困惑。所以,我最近写了一段代码,可以找到1000个素数中最大的回文。 代码是: def prime_large(number) arr_prime = [] Prime.each(number) do |x| new_arr_prime = arr_prime.push(x.to_s) updated = new_arr_prime.select { |y| y.reverse == y } end p u

我最近开始学习ruby,我对实例变量、局部变量和类变量感到困惑。所以,我最近写了一段代码,可以找到1000个素数中最大的回文。 代码是:

def prime_large(number)
  arr_prime = []
  Prime.each(number) do |x|
    new_arr_prime = arr_prime.push(x.to_s)
    updated = new_arr_prime.select { |y| y.reverse == y }
  end
  p updated.max 
end
p prime_large(1000)
我得到的错误是:

undefined local variable or method `updated' for main:Object (NameError)
我知道updated是prime的一个局部变量,因此我无法在它之外访问它,但我更改了代码,将其替换为@updated,如下所示:

def prime_large(number)
  arr_prime = []
  Prime.each(number) do |x|
    new_arr_prime = arr_prime.push(x.to_s)
    @updated = new_arr_prime.select { |y| y.reverse == y }
  end
  p @updated.max 
end
p prime_large(1000)
更改后,我得到了输出:

"929"
"929"

在我的代码中,没有创建类,我的实例变量(@updated)是如何工作的。我混淆了局部变量和实例变量。有人能给我解释一下它们的区别和工作原理吗?

在您的第一个示例中,您创建了一个局部变量
updated
,该变量只能在其定义的块的范围内访问。也就是说,它只在
Prime中可用。每个(数字)都结束

在第二个示例中,您创建了实例变量
@updated

在不创建类的情况下,我的实例变量(@updated)是如何更新的 工作

这是因为在Ruby中,一切都发生在某个对象的上下文中。即使您没有创建类,您也处于顶级上下文中,在object
main
的上下文中

因此,顶级中定义的任何实例变量都是此对象的实例变量
main

因此,回到您的问题,为了克服它,您只需在
素数之外定义
更新的
局部变量。每个(数字)do end
块:

def prime_large(number)
  arr_prime = []
  updated   = nil # initialize local varialbe
  Prime.each(number) do |x|
    new_arr_prime = arr_prime.push(x.to_s)
    updated = new_arr_prime.select { |y| y.reverse == y } # assign new value
  end
  p updated.max 
end
p prime_large(1000)
要测试它,您可以打开irb或撬开并自己检查:

self               # the object in the context of which you are currently
#=> main
self.class         # main is an instance of class Object, any methods defined
                   # within main are added to Object class as instance methods
#=> Object
instance_variables # list of it's instance variables, none yet created
#=> []
@variable = 1      # create and instance variable
#=> 1
instance_variables # now created variable occurs in the list of current object's instance variables
#=> [:@variable]
def hello; :hello end # define method
#=> :hello
self.class.public_instance_methods(false) # list instance methods defined in Object
#=> [:hello]

您现在想了解的是Ruby中的词法作用域。

解释如何解决这个问题(即:在块作用域之外初始化变量)可能会很有用。@Jordan我本来打算写这篇文章的,但我认为作用域的全部内容让它更直观。我将编辑答案。谢谢!