ruby类变量在development模式失效
分页中用到类变量,主要是用来标记“页码输入框”的id 如果一个页面有几个分页,“页码输入框”的id要是不同的才能分清是哪个要分页。使用类变量就是为了达到这个目的,让所有的对象实例共用一个变量,不必每次重新初始化变量。 类变量使用代码示例 1 require 'ruby-debug'
2 debugger
3 class Myclassvar
4 @@a=1
5 puts 1111111111111111111111
6 puts @@a
7
8 def testa
9 @@a=@@a+5
10 end
11 def testb
12 @@a=@@a+3
13 end
14 end
15
16 class Reclassvar < Myclassvar
17 def testa
18 @@a+=10
19 end
20 end
21
22 obj=Myclassvar.new
23 puts obj.testa
24 puts obj.testb
25 obj2=Myclassvar.new
26 puts obj2.testb
27 obj3=Reclassvar.new
28 puts obj3.testa
执行顺序是 3=>4=>5=>6=>8=>11=>16=>17=>22=>23=>9=>24=>12=>25=>26=>12……
本地测试类变量完全符合预想,但是项目中的分页用到的类变量却是每次都要初始化,一度郁闷中。结果是因为Rails开在development模式时配置中有config.cache_classes = false,所以我们每次不用重启服务就可以查看更新代码后的运行结果。而服务器上的程序是开启在production模式,其中config.cache_classes = true。这就是为什么类变量在development模式会失效,每次重新载入某个类时,它的所有类变量都会再次初始化。
相关文档:
Beginning Ruby from Novice To Perfessional
Head First Rails
Agile Web Development with Rails 3rd Edition(new)
Agile Web Development with Rails 3rd Edition
Agile Web Development with Rails 3rd Editio ......
When I try the command "gem install thrift" with Ruby 1.9.1, I got a compilation error with something related to a C function "strlcpy()".
Then I searched the web. It seems I am not alone and the community know it.
However I do not want to wait for official update, I want to ......
To get it done is not easy. I spent a whole day to figure out the various compatibility issues along the way out.
Now there still might be potential issues, but it works by my rough test.
Step 1: Install Apache Cassandra
You may know that the Ruby gem cassandra will do it for you.
  ......
The following is improved version of the code created by David Mullet, from
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fields
attr_wr ......
在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......