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模式会失效,每次重新载入某个类时,它的所有类变量都会再次初始化。
相关文档:
转自:http://developer.51cto.com/art/200912/170762.htm
Ruby字符串处理函数总结列表分享
Ruby字符串处理函数包括返回字符串长度函数;判断字符串中是否包含另一个串函数;字符串插入;字符串分隔,默认分隔符为空格等等。
str.length => integer
str.include? other_str
&nbs ......
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调用执行shell命令的方法
碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法:
1.Exec方法:
Kernel#exec方法通过调用指定的命令取代当前进程:
例子:
$ irb
>> exec 'echo " ......
转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
1.1.Rails
1.1 创建一个Rails应用程序
$ rails app_name
可选项:
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定,scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录
1.2 API Docume ......