Skip to content

Instantly share code, notes, and snippets.

View liwh's full-sized avatar
🎯
Focusing

robie lee liwh

🎯
Focusing
View GitHub Profile
@liwh
liwh / _notes.md
Created March 1, 2011 12:26 — forked from mislav/_notes.md

This gist is outdated

Homebrew people have since included this in a recipe.

$ brew update
$ brew install mongodb

( follow the instructions given )

~$ ARCHFLAGS='-arch i386 -arch x86_64'
~$ rvm install 1.8.7 --debug --reconfigure -C --enable-shared=yes
~$ wget http://sourceforge.net/projects/rubycocoa/files/RubyCocoa/1.0.0/RubyCocoa-1.0.0.tar.gz/download
~$ tar xzf RubyCocoa-1.0.0.tar.gz && rm RubyCocoa-1.0.0.tar.gz && cd RubyCocoa-1.0.0
~/RubyCocoa-1.0.0$ ruby install.rb config --build-universal=yes
~/RubyCocoa-1.0.0$ ruby install.rb setup
~/RubyCocoa-1.0.0$ sudo ruby install.rb install
@liwh
liwh / gist:831209
Created February 17, 2011 07:14
introduce the feature of mongodb
1._id and Objectids
每一个document在MongoDB中保存时,都必须有一个"_id"键,这个键可以为任何类型,默认的是一个ObjectId.在一个单独的collection中,每一个document的"_id"必须是唯一的,这样,collection里的每一个document都能被唯一识别.这个relational database也是一样的.
2.ObjectIds
ObjectId是_id的默认类型.它被设计为轻量级的,与此同时,它也很容易生成全局唯一的方式,在不同的机器上.这就是MongoDB利用ObjectIds,而不用传统的,如自动增长的主键的原因.传统的方式很难而且耗时去同步自动增加的主键值在多个服务中.因为MongoDB是从分布式的数据中设计出来的.处理多节点是一个重要的考虑的因素.这个ObjectId类型,如我们看到的,很容易在共享的环境下生成.也类似传统数据库中uuid的主键生成方式.
ObejctIds利用12个字节来存储,这里用24位十六进制来表示.
变量不同种类反映出它们作用的范围不同
类里面的不包含在方法里初始赋值的,有@@ 眼镜之称的是类变量,可以作用到方法里,方法们就能访问到
如果没有定义@@ 的,即使不包含到方法里,方法也访问不到
定义到方法里的,只有该方法能访问到,别的方法和类也访问不到
最宽松的是用美金开头的全局变量,只要放到程序的任何地方,都能被访问到
(你说好不好? 失去了控制不能保证访问受限,谁都能改,肯定容易出错)
@liwh
liwh / gist:827008
Created February 15, 2011 02:36
线程安全的文件读写 线程安全的文件读写
原子写文件:
当有一个线程对文件进行写的操作的时候,而此时,有其他多个线程对这个文件进行读的操作的时候,如果以见得方式对文件进行写操作的时候.这时,就会造成某些线程会看到不完成的文件.
所以,我们可以利用原子方法automic_write.首先,我们将写的文件存在一个临时文件中,然后,再将临时文件去覆盖我们需要对那个进行写操作的文件.
require 'fileutils'
def atomic_write(path, temp_path, content)
File.open(temp_path, 'w+') do |f|
f.write(content)
end
FileUtils.mv(temp_path, path)
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
这是的 enviroment.rb 中的注释。如果自动加载就放在config/initializers。
很多东西只要留心就好了。
当然,如果要深入,就全面了解一下rails的加载过程吧。原则上,第三方(我们自己开发的东西,比如你这个文件或方法)开发的组件,是可以放在lib下了,只不过要配置一下。看看那些开源的项目,很多对rails自身的修改都习惯自己建立一个lib/patches包,方便管理。
只不过在用的使用,先要使用require "patches/youlibfile.rb"(前面没有lib/)就好了。
第三种方案,就是自己构建一个文件夹了,如在app的平级添加mylibs文件夹,在enviroment.rb使用config.load_paths += %w(#{RAILS_ROOT}/mylibs)就ok了
## 获取当前的文件名称
puts "__FILE__ == #{__FILE__}"
Result: __FILE__ == test01/motorcycletest.rb
## 获取当前文件的目录名称
puts "File.dirname(__FILE__) == #{File.dirname(__FILE__)}"
Result: File.dirname(__FILE__) == test01
## 获取当前文件的完整名称
当要获取完整的路径时需要require 'pathname',代码如下:
>> c.errors.add_on_blank('instructor','not be blank')
=> ["instructor"]
>> c.errors
=> #<ActiveRecord::Errors:0xb01f150c @errors={"instructor"=>["not be blank"]}, @base=#<Conversation id: "3e7f4481-e96b-012d-7fdc-042b2b5ce4fb", state: 10, tag: "20101214-2-00022", title: "死亡索赔", aim: 1, emergency: 1, security: 1, kind_id: 1, content: "当事人利爱琼称有29人来我厂索赔,事�...", record_id: nil, district: "440310008000", ownerdistrict: "440310008000", sub_kind_id: 199, place: "坑梓", source: 3, reportor_id: "3e7f4480-e96b-012d-7fdc-042b2b5ce4fb", reported_at: "2010-12-14 12:46:00", levels: 2, finishdate: "2011-01-27 00:00:00", advice: "请司法所处理,并将处理结果于15个工作...", remark: nil, flag: 1, creator_id: "57ec3557-c4dc-4ac8-87a6-a13ad4104686", modifier_id: "57ec3557-c4dc-4ac8-87a6-a13ad4104686", dispatched_at: "2010-12-16 18:29:35", finished_at: nil, created_at: "2010-12-14 12:47:57", updated_at: "2010-12-31 17:59:31", count: nil, grid_id: nil, building_id: nil, warndate: "2011-01-25 00:00:00", dealdistrict: "440310008000", instruct: "同意拟办意见."
@liwh
liwh / gist:779026
Created January 14, 2011 02:02
going crazy wit proc
http://iain.nl/2010/02/going-crazy-with-to_proc/
当使用mysql,我们利用actasferret作全文检索的时候,会发现有时候,我们重建索引会失效,查看日志会发现
using mysql specific batched find :all. Turn off with :mysql_fast_batches => false if you encounter problems (i.e. because of non-integer UUIDs in the id column) 的时候,这时候,我们需要将:mysql_fast_batches => false加到模型的acts_as_ferret中去。