本文介绍了实例变量可以保留多长时间?在Rails中?在Java中?在PHP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我实例化一个类并在Rails控制器或模型使用的Ruby模块中设置实例变量,那么该实例变量可以保留多长时间?是否需要在会话中显式存储变量以在请求之间持久化?

If I instantiate a class and set an instance variable in a Ruby module used by a Rails controller or model, how long does that instance variable persist? Does a variable need to be explicitly stored in the session to persist between requests?

Rails在这方面与Java或PHP相比如何?

How does Rails compare in this regard to Java or PHP?

推荐答案

在Ruby on Rails中,它始终取决于范围和定义的位置.

In Ruby on Rails, it always depends on the scope and where it's defined.

例如,在您的环境和配置文件中定义的对象/实例变量将始终存在.一个简单的示例是ActiveMerchant支付网关,该网关在environment.rb文件中定义,并且对于每个请求都存在.

For example, objects/instance variables defined in your environment and configuration files, will persist always. A simple example of this is ActiveMerchant payment gateways, which are defined in the environment.rb file and are present for every request.

对于控制器,它仅针对该HTTP请求显示对象和实例变量.

In case of controllers, it present for that HTTP request alone, the object and the instance variables.

这篇关于实例变量可以保留多长时间?在Rails中?在Java中?在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:30