Ruby on Rails 中的@变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14319347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:17:46  来源:igfitidea点击:

@ variables in Ruby on Rails

ruby-on-railsrubyvariables

提问by OneZero

What's the difference between @titleand title? Since both of them can be variable names. Also, how do I decide which kind of variable I should use? With @or not?

@title和 和有title什么区别?因为它们都可以是变量名。另外,我如何决定应该使用哪种变量?有@没有?

回答by Peter Rasmussen

titleis a local variable. They only exists within its scope (current block)

title是局部变量。它们只存在于其范围内(当前块)

@titleis an instance variable - and is available to all methods within the class.

@title是一个实例变量 - 可用于类中的所有方法。

You can read more here: http://strugglingwithruby.blogspot.dk/2010/03/variables.html

您可以在此处阅读更多信息:http: //strugglingwithruby.blogspot.dk/2010/03/variables.html

In Ruby on Rails - declaring your variables in your controller as instance variables (@title) makes them available to your view.

在 Ruby on Rails 中 - 将控制器中的变量声明为实例变量 ( @title) 使它们可用于您的视图。

回答by joscas

Use @titlein your controllers when you want your variable to be available in your views.

@title当您希望您的变量在您的视图中可用时,在您的控制器中使用。

The explanation is that @titleis an instance variable and titleis a local variable and rails makes instance variables from controllers available to views. This happens because the template code (erb, haml, etc) is executed within the scope of the current controller instance.

解释是它@title是一个实例变量并且title是一个局部变量,rails 使控制器中的实例变量可用于视图。发生这种情况是因为模板代码(erb、haml 等)是在当前控制器实例的范围内执行的。

回答by GSP

The difference is in the scope of the variable. The @version is available to all methods of the class instance.

区别在于变量的范围。@version 可用于类实例的所有方法。

The short answer, if you're in the controller and you need to make the variable available to the view then use @variable.

简短的回答,如果您在控制器中并且需要使变量可用于视图,则使用@variable.

For a much longer answer try this: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html

对于更长的答案试试这个:http: //www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html

回答by Prabhakar Undurthi

@variables are called instance variables in ruby. Which means you can access these variables in ANY METHOD inside the class. [Across all methods in the class]

@variables 在 ruby​​ 中被称为实例变量。这意味着您可以在类内的任何方法中访问这些变量。[跨类中的所有方法]

Variables without the @symbol are called local variables, which means you can access these local variables within THAT DECLARED METHOD only. Limited to the local scope.

没有@符号的变量称为局部变量,这意味着您只能在那个声明的方法中访问这些局部变量。限于本地范围。

Example of Instance Variables:

实例变量示例:

class Customer
  def initialize(id, name, addr)
    @cust_id = id
    @cust_name = name
    @cust_addr = addr
  end

  def display_details
    puts "Customer id #{@cust_id}"
    puts "Customer name #{@cust_name}"
    puts "Customer address #{@cust_addr}"
  end
end

In the above example @cust_id, @cust_name, @cust_addrare accessed in another method within the class. But the same thing would not be accessible with local variables.

在上面的例子中@cust_id@cust_name,@cust_addr是在类中的另一个方法中访问的。但是同样的事情不能用局部变量访问。

回答by Hearen

A tutorial about What is Variable Scope?presents some details quite well, just enclose the related here.

关于什么是变量范围的教程一些细节介绍得很好,只需在此处附上相关内容。


+------------------+----------------------+
| Name Begins With |    Variable Scope    |
+------------------+----------------------+
| $                | A global variable    |
| @                | An instance variable |
| [a-z] or _       | A local variable     |
| [A-Z]            | A constant           |
| @@               | A class variable     |
+------------------+----------------------+

回答by Joe Hilton

A local variable is only accessible from within the block of it's initialization. Also a local variable begins with a lower case letter (a-z) or underscore (_).

局部变量只能从它的初始化块内访问。此外,局部变量以小写字母 (az) 或下划线 (_) 开头。

And instance variable is an instance of selfand begins with a @Also an instance variable belongs to the object itself. Instance variables are the ones that you perform methods on i.e. .sendetc

并且实例变量是一个实例self并且以a开头,@也是一个实例变量属于对象本身。实例变量是您在 ie.send等上执行方法的变量

example:

例子:

@user = User.all

The @useris the instance variable

@user是实例变量

And Uninitialized instance variables have a value of Nil

未初始化的实例变量的值为 Nil

回答by John Beynon

@ variables are instance variables, without are local variables.

@变量是实例变量,没有是局部变量。

Read more at http://ruby.about.com/od/variables/a/Instance-Variables.htm

http://ruby.about.com/od/variables/a/Instance-Variables.htm阅读更多