Skip to content

Instantly share code, notes, and snippets.

View liwh's full-sized avatar
🎯
Focusing

robie lee liwh

🎯
Focusing
View GitHub Profile
## 获取当前的文件名称
puts "__FILE__ == #{__FILE__}"
Result: __FILE__ == test01/motorcycletest.rb
## 获取当前文件的目录名称
puts "File.dirname(__FILE__) == #{File.dirname(__FILE__)}"
Result: File.dirname(__FILE__) == test01
## 获取当前文件的完整名称
当要获取完整的路径时需要require 'pathname',代码如下:
# 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了
@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)
变量不同种类反映出它们作用的范围不同
类里面的不包含在方法里初始赋值的,有@@ 眼镜之称的是类变量,可以作用到方法里,方法们就能访问到
如果没有定义@@ 的,即使不包含到方法里,方法也访问不到
定义到方法里的,只有该方法能访问到,别的方法和类也访问不到
最宽松的是用美金开头的全局变量,只要放到程序的任何地方,都能被访问到
(你说好不好? 失去了控制不能保证访问受限,谁都能改,肯定容易出错)
@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位十六进制来表示.
~$ 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 / _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 )

when we use zsh ,we may get some problem on the keybord shortcuts.for example ,the shortcuts ctrl+a is going the head of a line ,but in zsh ,it is not occured! so we must change the bindkey.
~ ❯ bindkey
"^A"-"^C" self-insert
"^D" list-choices
"^E"-"^F" self-insert
"^G" list-expand
"^H" vi-backward-delete-char
"^I" expand-or-complete
"^J" accept-line
gem list -d // show the gems and the description of gem
this.vtip = function() {
this.xOffset = -10;
this.yOffset = 10;
//为class为vtip的增加鼠标移进和移出时间
$(".vtip").unbind().hover(
function(e) {
this.t = this.title;
this.title = '';