Showing posts with label Variable. Show all posts
Showing posts with label Variable. Show all posts

Thursday, March 15, 2007

Instance variables and object state

Information and data associated with a particular object is called the state of the object.
我们应该能够做到:set/reset and read the state of an object。


The instance variable enables individual objects to remember state. Instance variables work much like other variables: You assign values to them, and you read those values back.
However,instance variables have a few differences.
1, Instance variable names always start with @. This enables you to recognize an instance variable at a glance.
2, Instance variables are only visible to the object to which they belong.
3, An instance variable is initialized in one method definition, inside a particular class.

Instance这个词意味着什么呢?意味着class instance,也就是object。所以,instance variable是和class没有关系的,只和一个具体的object有关系。
根据上面的第三条描述,一个instance variable是在一个class的method中初始化的。所以 ,在定义一个class的时候,写在method里面的才是instance varialbe,才能够被每个instance所使用。
但是,一个class本身也是一个object,所以,在method之外的variable就是该class这个object本身自己的instance variable了,与这个class的instance们没有关系。

Unlike a local variable, the instance variable @ivar retains the value assigned to it even after the method in which it was initialized has terminated. This property of instance variables—their survival across method calls—makes them suitable for maintaining state in an object.

我们常常要把arguments赋给Instance Variable。我对自己的编程要求是: Instance Varialbe的名字必须与argument的名字一样,例如:@word = word

Local Variable

Local variables are variables that hold their value only during the execution of a particular section of code. They’re called local precisely because once program execution leaves the scope where the variable was created, the variable’s name no longer has any meaning.

Local variables give you a kind of scratch-pad facility. You can use, say, the variable name x in more than one place; as long as those places have different scopes, the two x variables are treated as completely separate.

Local variables can come into being in either of two ways:
■ Through assignment: x = object
■ As an entry in the method’s argument list, initialized when the method iscalled

Variables in Ruby don’t hold object values, rather, it contains a reference to an object.

Local variables have the quality of barewords; they must start with either a lowercase letter or the underscore character (_), and they must consist entirely of letters, numbers, and underscores.
我给自己制定的命名规则是:Local variable一律用“_”开头。例如:_one_apple

Here’s how Ruby decides what it’s seeing when it encounters a bareword:
1. If there’s an equal sign (=) to the right of the bareword, it’s a local variable undergoing an assignment.
2. If the bareword is a keyword, it’s a keyword.
3. Otherwise, the bareword is assumed to be a method call.

Friday, March 09, 2007

Basics of Veiw

A View is an ERb program that shares data with Controllers through mutually accessible Variables. 首先,view是一个ERb程序。然后,View同Controller共享数据(通过Variable)。

Controller actions and View template files are connected through naming conventions:
An incoming request for the picture/display Action triggers execution of the display method in the picture Controller, followed by rendering of the picture/display.rhtml file in the Views area.

Basicly, the Controller prepares the data and stores it in Variables, and those Variables are used in the ERb file corresponding to that Action.

Ruby Variables

The composer controller stashes a particular composer into a variable called @composer. The values contained in these variables are available to the ERb code in the respective views.