之前在用 QS,现在追求简单,用系统自带的 Spotlight 了。
举个实际的例子吧,在什么情况下?
def merge_string(long, short)
result = []
long_size = long.size
i = 0
short.each_char do |char|
result << long[i]
result << char
i += 1
end
result.join('') << long[i..long_size]
end
我的思路很简单。
发现问题了,是我的测试数据不一致导致的。上面的例子中,pg 有一万条样本数据,mysql 只有 1 千条,估计这时候 mysql 觉得数据量太小了,不用索引也很快?
刚刚也怀疑数据量的问题,重新测了下,在 mysql 填充一万条样本数据的情况下,也会用到索引:
mysql> explain select * from topics order by updated_at desc limit 15;
+----+-------------+--------+-------+---------------+----------------------------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+----------------------------+---------+------+------+-------+
| 1 | SIMPLE | topics | index | NULL | index_topics_on_updated_at | 8 | NULL | 15 | |
+----+-------------+--------+-------+---------------+----------------------------+---------+------+------+-------+
1 row in set (0.00 sec)
动手写代码,开发自己想要的功能。如果写不出,回去看视频。记笔记什么的,作用不大。
注释很容易跟不上代码的重构节奏。
我认为把每个函数写的简短一些,就不用写注释了。
找到了 content_for 的文档,里面说的很清楚:http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
module ApplicationHelper
def cascading_block(*args)
block_name,default_tpl = args
content_for?(block_name) ? content_for(block_name) : render(partitial:"default_tpl")
end
end
#3 楼 @natsuki 看看这个:http://stackoverflow.com/questions/6781407/rendering-a-content-for-block-in-a-helper
那个人说答案是在 helper 中再次调用 content_for(:block_name),我没有验证,你可以试试。
说明楼主还不为钱犯愁,应该感到幸福啊!
正好遇到这个 bug,感谢楼主分享。
比较奇怪的是,ruby china 的备案号,现在又不显示了。。
用 block 做参数来创建一个 hash 的话,在每次用 Hash#[] 方法来读取一个 key 时,如果 Key 找不到,会自动调用你传入的 block。
具体到你的例子, hash[:one] << "uno"
这行代码,发生的情况如下:
{|hash, key| hash[key] = [] }
, 效果就是hash[:one] = []
,然后返回 hash[:one] 的值,也就是一个数组于是,hash[:one] 的值,就变成了 ["uno"]
Hash#fetch 与 Hash#[] 是不同的方法,实现不同。
不同的类,实现的 clone 方法不一样。Array 对第一层对象做深度拷贝,但是嵌套的对象还是 shadow copy。试试这个:
ruby-1.9.2-p0 > a = Array.new
[]
ruby-1.9.2-p0 > a << ['first']
[
[0] [
[0] "first"
]
]
ruby-1.9.2-p0 > b = a.clone
[
[0] [
[0] "first"
]
]
ruby-1.9.2-p0 > b[0] << 'second'
[
[0] "first",
[1] "second"
]
ruby-1.9.2-p0 > a
[
[0] [
[0] "first",
[1] "second"
]
]
发工资吗?
所有发邮件的同学,我都邀请加入了。