Peterfei

上主是我的牧者,我实在一无所缺


  • 首页

  • 归档

  • 标签

ruby类扩展混入

发表于 2014-10-09   |  

Mac下sublime3快捷键备忘

发表于 2014-09-27   |  

打开/前往

  • ⌘^A (对齐)
  • Alt鼠标单击 [多行选择]
  • ⌘T [前往文件]
  • ⌘⌃P | 前往项目 |
  • ⌘R | 前往 method |
  • ⌘⇧P | 命令提示 |
  • ⌃G | 前往行 |
  • ⌃ ` | python 控制台 |

    编辑

  • ⌘L | 选择行 (重复按下将下一行加入选择) |

  • ⌘D | 选择词 (重复按下时多重选择相同的词进行多重编辑) |
  • ⌃⇧M | 选择括号内的内容 |
  • ⌘⇧↩ | 在当前行前插入新行 |
  • ⌘↩ | 在当前行后插入新行 |
  • ⌃⇧K | 删除行 |
  • ⌘KK | 从光标处删除至行尾 |
  • ⌘K⌫ | 从光标处删除至行首 |
  • ⌘⇧D | 复制(多)行 |
  • ⌘J | 合并(多)行 |
  • ⌘KU | 改为大写 |
  • ⌘KL | 改为小写 |
  • ⌘ / | 注释 |
  • ⌘⌥ / | 块注释 |
  • ⌘Y | 恢复或重复 |
  • ⌘⇧V | 粘贴并自动缩进 |
  • ⌃ space | 自动完成(重复按下选择下一个提示) |
  • ⌃M | 跳转至对应的括号 |

查找/替换

  • ⌘F | 查找 |
  • ⌘⌥F | 替换 |
  • ⌘⌥G | 查找下一个符合当前所选的内容 |
  • ⌘⌃G | 查找所有符合当前所选的内容进行多重编辑 |
  • ⌘⇧F | 在所有打开的文件中进行查找 |

ruby api设计

发表于 2014-09-27   |  

#先看一段source code

1
2
3
4
5
6
7
8
9
10
11
f = File.open("myfile.txt", 'w')
f.write("Lorem ipsum dolor sit amet")
f.write("Lorem ipsum dolor sit amet")
f.close
||
\/
# using block
File.open("myfile.txt", 'w') do |f|
f.write("Lorem ipsum dolor sit amet")
f.write("Lorem ipsum dolor sit amet")
end

#如上,是使用block后的效果,再来一段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# without code block
def send_message(msg)
socket = TCPSocket.new(@ip, @port) # Pre-
socket.puts(msg)
response = socket.gets
ensure
socket.close # Post-
end
end
||
\/
# with code block
def send_message(msg)
connection do |socket|
socket.puts("foobar")
socket.gets
end
end
def connection
socket = TCPSocket.new(@ip, @port) # Pre-
yield(socket)
ensure
socket.close # Post-
end
end

#Module 的使用

1
2
3
4
5
6
7
8
9
module Mixin
def foo
puts "foo"
end
end
class A
include Mixin
end
A.new.foo # foo

.使用extend(Mod)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Mixin
def foo
puts "foo"
end
class A
extend Mixin
end
end
A.extend(Mixin)
A.foo # class method
the same as
class << A
include Mixin
end

.obj.extend(Mod)
如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Mixin
def foo
puts "foo"
end
end
a = "test"
a.extend(Mixin)
a.foo # foo
the same as
class << a
include Mixin
end
b = "demo"
b.foo # NoMethodError

.Mixin with class methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Mixin
def foo
puts "foo"
end
module ClassMethods
def bar
puts "bar"
end
end
end
class MyClass
include Mixin
extend Mixin::ClassMethods
end

等同于如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Mixin
# self.included is a hook method
def self.included(base)
base.extend(ClassMethods)
end
def foo
puts "foo"
end
module ClassMethods
def bar
puts "bar"
end
end
end
class MyClass
include Mixin
end

等同于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module Mixin
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
end
module InstanceMethods
def foo
puts "foo"
end
end
module ClassMethods
def bar
puts "bar"
end
end
end
class MyClass
include Mixin
end

Magento 该如何多语言翻译

发表于 2014-09-25   |  

一般的,登陆后台,e.g.选择[目录]->[管理产品]->[choose store view],如图示:
choose store view

就可以编辑中文内容,同理,选择[目录]->[管理产品]->[choose store view],选择英文,就可以编辑英文产品。
编辑’CMS’,如[页面],选择一静态页面,如[About Us],点编辑后,按”ctrl”健,在商店界面,点’英文’,界面会着色,之后选内容项,就可以编辑英文内容,同理,再新建一页面,复制’About Us’的页面信息,如标题,网址等,按”ctrl”健,在商店界面,点’中文’,界面会着色,之后选内容项,就可以编辑中文内容,前台就已经对应生成中英文双语的关于我们了,是不是很Cool。
choose store view

Rails中的子查询

发表于 2014-09-22   |  

###大致是这样子的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def favarate_post options={}
subselect = Favarate.send(
:construct_finder_sql,
:select =>"post_id",
:conditions=>{:blog_id=>self.id},
:order=>"published_at desc",
:limit => option[:limit]||10,:offset=>option[:offset]
)
Post.find(:all,:conditions =>'posts.id in #{subselect}',:order =>"published_at DESC")
end
```
construct_finder_sql 同下句SQL类似:
```sql
SELECT * FROM posts WHERE posts.id IN (
SELECT post_id FROM favorites WHERE blog_id = 42 ORDER BY published_at DESC LIMIT 10 OFFSET 10
) ORDER BY published_at DESC

1…141516
peterfei

peterfei

peterfei|技术|上主是我的牧者

80 日志
14 分类
62 标签
RSS
github
© 2025 peterfei
由 Hexo 强力驱动
主题 - NexT.Mist