@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)


問題描述

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

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

‑‑‑‑‑

參考解法

方法 1:

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

@title is an instance variable ‑ and is available to all methods within the class.

You can read more here: 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.

方法 2:

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

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

方法 3:

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     |
+‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑+‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑+

方法 4:

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

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

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

方法 5:

@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 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_addr are accessed in another method within the class. But the same thing would not be accessible  with local variables.

(by OneZeroPeter RasmussenjoscasHearenGSPPrabhakar Undurthi)

參考文件

  1. @ variables in Ruby on Rails (CC BY‑SA 3.0/4.0)

#Variables #ruby-on-rails #ruby






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論