实例变量的实际目的是什么?

时间:2022-11-16 09:19:35

I'm new to ruby rails, and I'm struggling to understand what the purpose of instance variables are.

我是ruby rails的新手,我很难理解实例变量的用途。

What is the point of making a variable into an instance variable?

将变量变成实例变量有什么意义?

Basically why would I do this:

基本上我为什么要这样做:

class User
   def initialize(var1)
     @name = var1
   end
end

over this:

class User
   def initialize(var1)
     name = var1
   end
end

And in what types of situations would I want to use an instance variable over just a regular variable?

在什么类型的情况下,我想要使用实例变量而不仅仅是常规变量?

3 个解决方案

#1


0  

The practical use for this is that if you need a variable to persist across function calls, make it an instance variable.

实际应用是,如果需要一个变量来保持跨函数调用,请将其设为实例变量。

after you define the instance variable, you can call the value of this on another function and you will have the value.

在定义实例变量之后,可以在另一个函数上调用this的值,您将获得该值。

In general an instance variable is local and persisted inside an instance of an object, whereas a local variable is only local and persisted inside a function/object/block scope.

通常,实例变量是本地的并且持久存储在对象的实例中,而局部变量只是本地变量并且持久存储在函数/对象/块范围内。

in your example if you have another method on your user class, you can use the value of @name on that other method, but coulnd't use var1 outside of initialize method.

在你的例子中,如果你的用户类上有另一个方法,你可以在另一个方法上使用@name的值,但是不能在initialize方法之外使用var1。

class User
   def initialize(var1)
     @name = var1
   end

   def greetings
     "hello #{@name}"
   end    
end

#2


-1  

class People
  def age
    @age
  end

  def age= age
    @age = age
  end
end

def show people
  age = people.age || '22'
  p "Hello, my age is #{age}"
end

people = People.new
show people
=> 'Hello, my age is 22'
age
=> NameError: undefined local variable or method 'age' for main:Object
user.age = 21
greet people
=> 'Hello, my age is 21'
@age
=> nil

Notice that both variable types are local to a specific context though. @age is defined within the people instance, so when you call people.age you are calling the age function in the age instance, in which @age is defined. age is only defined in the show function, so when you call p "Hello, my age is #{age}" you are able to get a value for age because you are within the scope in which it is defined.

请注意,两个变量类型都是特定上下文的本地变量。 @age是在people实例中定义的,因此当您调用people.age时,您将在age实例中调用age函数,其中定义了@age。 age仅在show函数中定义,因此当您调用p“Hello,我的年龄为#{age}”时,您可以获得年龄值,因为您在定义范围内。

#3


-2  

Because your instance variable will be accessible to your view code, including within forms and passing data to paths. That's probably the most common, practical use case for them. In your example, I could do the following:

因为您的视图代码可以访问您的实例变量,包括在表单中并将数据传递给路径。这可能是他们最常见,最实用的用例。在您的示例中,我可以执行以下操作:

<p>User: <%= "#{@name}" %></p>

And, instead of seeing "@name" in my corresponding paragraph in the rendered H.T.M.L, you'd see the value of that instance variable - whereas the inverse:

并且,不是在渲染的H.T.M.L中的相应段落中看到“@name”,而是看到该实例变量的值 - 而反之:

<p>User: <%= "#{name}" %></p>

...will probably result in an ActionView error, since "name" is undefined, according to it.

...可能会导致ActionView错误,因为“name”是未定义的。

#1


0  

The practical use for this is that if you need a variable to persist across function calls, make it an instance variable.

实际应用是,如果需要一个变量来保持跨函数调用,请将其设为实例变量。

after you define the instance variable, you can call the value of this on another function and you will have the value.

在定义实例变量之后,可以在另一个函数上调用this的值,您将获得该值。

In general an instance variable is local and persisted inside an instance of an object, whereas a local variable is only local and persisted inside a function/object/block scope.

通常,实例变量是本地的并且持久存储在对象的实例中,而局部变量只是本地变量并且持久存储在函数/对象/块范围内。

in your example if you have another method on your user class, you can use the value of @name on that other method, but coulnd't use var1 outside of initialize method.

在你的例子中,如果你的用户类上有另一个方法,你可以在另一个方法上使用@name的值,但是不能在initialize方法之外使用var1。

class User
   def initialize(var1)
     @name = var1
   end

   def greetings
     "hello #{@name}"
   end    
end

#2


-1  

class People
  def age
    @age
  end

  def age= age
    @age = age
  end
end

def show people
  age = people.age || '22'
  p "Hello, my age is #{age}"
end

people = People.new
show people
=> 'Hello, my age is 22'
age
=> NameError: undefined local variable or method 'age' for main:Object
user.age = 21
greet people
=> 'Hello, my age is 21'
@age
=> nil

Notice that both variable types are local to a specific context though. @age is defined within the people instance, so when you call people.age you are calling the age function in the age instance, in which @age is defined. age is only defined in the show function, so when you call p "Hello, my age is #{age}" you are able to get a value for age because you are within the scope in which it is defined.

请注意,两个变量类型都是特定上下文的本地变量。 @age是在people实例中定义的,因此当您调用people.age时,您将在age实例中调用age函数,其中定义了@age。 age仅在show函数中定义,因此当您调用p“Hello,我的年龄为#{age}”时,您可以获得年龄值,因为您在定义范围内。

#3


-2  

Because your instance variable will be accessible to your view code, including within forms and passing data to paths. That's probably the most common, practical use case for them. In your example, I could do the following:

因为您的视图代码可以访问您的实例变量,包括在表单中并将数据传递给路径。这可能是他们最常见,最实用的用例。在您的示例中,我可以执行以下操作:

<p>User: <%= "#{@name}" %></p>

And, instead of seeing "@name" in my corresponding paragraph in the rendered H.T.M.L, you'd see the value of that instance variable - whereas the inverse:

并且,不是在渲染的H.T.M.L中的相应段落中看到“@name”,而是看到该实例变量的值 - 而反之:

<p>User: <%= "#{name}" %></p>

...will probably result in an ActionView error, since "name" is undefined, according to it.

...可能会导致ActionView错误,因为“name”是未定义的。