- 浏览: 243210 次
- 性别:
- 来自: 杭州
最新评论
-
zhangyou1010:
回去倒立去,哈哈。
作为一个程序员,身体很重要! -
Hooopo:
Ruby MetaProgramming is all abo ...
Metaprogramming Ruby -
orcl_zhang:
yiqi1943 写道LZ现在上学还是工作呢工作好多年了。不过 ...
2011年 -
yiqi1943:
LZ现在上学还是工作呢
2011年 -
tjcjc:
query cache
就是一个简单的hash
key就是sq ...
Rails sql延迟加载和自带缓存
文章列表
Chapter 6. Files and Directories
创建目录结构的代码
# create_tree.rb
def create_tree(directories, parent=".")
directories.each_pair do |dir, files|
path = File.join(parent, dir)
Dir.mkdir path unless File.exists? path
files.each do |filename, contents|
if filename.respond ...
Recipe 5.11. Choosing Randomly from a Weighted List
Store the elements in a hash, mapped to their relative probabilities. The following code will work with a hash whose keys are mapped to relative integer probabilities:
def choose_weighted(weighted)
sum = weighted.inject(0) do |sum, item_and_wei ...
Recipe 1.19. Validating an Email Address
===
Discussion
Most email address validation is done with naive regular expressions like the ones given above. Unfortunately, these regular expressions are usually written too strictly, and reject many email addresses. This is a common source of frustration f ...
Recipe 5.5. Using an Array or Other Modifiable Object as a Hash Key
Suppose you want a reliably hashable Array class. If you want this behavior universally, you can reopen the Array class and redefine hash to give you the new behavior. But it's safer to define a subclass of Array that implements a re ...
Creating a Hash with a Default Value
first_letter_hash = Hash.new { |hash, key| hash[key] = [] }
text.split(/\W+/).each { |word| first_letter_hash[word[0,1].downcase] << word }
first_letter_hash
# => {"m"=>["mainly"], "p"=>["plain"], "f ...
Hash
1. Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the only time you should need to do this is if your class also overrides Object#==. If two objects are consid ...
ActiveRecord: Skipping callbacks like after_save or after_update
Active Records provides callbacks, which is great is you want to perform extra business logic after (or before) saving, creating or destroying an instance of that model.
However, there are situations where you can easily fall into the ...
二,oop
每一个函数都包含了一个prototype属性,这个属性指向了一个prototype对象
Every function has a prototype property that refers to a predefined prototype object
看过一篇文章介绍说javascript中对象的prototype属性相当于java中的static变量,可以被这个类 ...
recipes 2 :Making Your Own JavaScript Helper
All these magic helpers are really not so magical after all. All they do is generate JavaScript and HTML fragments for us. It’s just text generation, but the text happens to be JavaScript.
==
javascript help生成我们所需要的html和javascript代码片段。
As we saw in the recipe, we can see what action the Ajax control is attempting to call by looking at our web server log.But since it’s making a POST, we can’t see the parameters in the log.How do you know what parameters an autogenerated form is expecting without reading through piles of source code? ...
Recipe 8.16. Making a Copy of an Object
The downside of dup is that it creates a new instance of the object's original class. If you open up a specific object and give it a singleton method, you implicitly create a metaclass, an anonymous subclass of the original class. Calling dup on the object will ...
这是我在实际中遇到的问题.
废话不多说,直接上代码。
在out里
has_many :out_items
def pick(os)
update_attributes(os)
out_items.each(&:change_placement)
end
def out_item_attributes=(out_item_attributes)
out_item_attributes.each do |key,value|
OutItem.find_by_id(key).update_attributes(value)
...
本ID从来只觉得自己说的不过是一家之言,言、行合一,本ID将自己的生命记录下来,也是言的一部分。
实证,一切必须如此。人,生而受骗中,你的文化、生存前提都构成你生命系统的所谓公理。而公理,往往就是骗局。
人生,真要活明白,前面30、40年,都要破这骗局,当然,有历史以来,真能办到这事的人,估计也没几个。绝大多数的人,不过在受骗中终其一生。
现代人,更是如此。我们不过首先都是耳食之辈,最终选择了一种信的东西,然后如抽鸦=?片一样一生了。
绝大多数的人,循规蹈矩地一生;而风云际会之时,又有所谓英雄、革命;这构成历史。有些人,希望穿越人的阶梯,因此而有了修行。
如果人只是人,那么,人如何去安顿只不过是 ...
@client_ip = request.env["HTTP_X_FORWARDED_FOR"] #取得使用者IP位址資訊
@client_browser = request.env["HTTP_USER_AGENT"] #取得使用者瀏覽器資訊
@http_referer = request.env["HTTP_REFERER"] #取得使用者來源(referer)
@client_language = request.env["HTTP_ACCEPT_LANGUAGE"] #取得使用者支援語系
@serve ...
class Picture < ActiveRecord::Base
has_many :most_recent_comments, :class_name => 'Comment', :order => 'id DESC', :limit => 10
end
Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments.