ruby入门
Ruby API查询
Ruby 正则表达式查询
ruby技巧
字符串转数组
s = "a=1&b=2"
a = s.split(/&|=/)
#["a", "1", "b", "2"]
数组转为哈希
a = ["a", "1", "b", "2"]
#*号可以将数组中的字符串展开
Hash[*a]
#{"a"=>"1", "b"=>"2"}
char的ASCII值
#ASCII值转为char
120.chr
#char转为ASCII值
'x'.ord
#查看字符串的ASCII值
"love".chars.map(&:ord)
字符编码转换
require 'iconv'
#conv(to, from, str)
Iconv.conv('gb2312', 'utf-8', '中国')
字符串转换为类
Kernel.const_get 'Integer'
Iconv
<script type="text/javascript"> var disqus_url = "http://saberma.me/rails/2009/12/18/ruby-tips.html"; var disqus_developer = 1; </script>
2009-09-25
# 接调用项目中的类和方法
script/console
#查看所有的用户
User.all
#访问某个地址,用于调试
app.get '/users'
app.cookies
app.flash
app.request
app.response
# 检查服务器后台运行情况
tail -f log/development.log
# 数据库迁移(test)
rake db:migrate RAILS_ENV=test
#数据库回滚3步
rake db:migrate:redo STEP=3
#查看ruby环境
puts '$:'
#查看gem环境
gem environment
gem e
h2. 在View中使用输出字符串
#假设在haml中需要调用foo,你使用
- foo do
hello world
#在foo中需要输出某些字符串,如saberma,则可以利用concat
def foo
concat 'saberma'
yield
end
另外,capture也是个好东西:) "capture api":http://apidock.com/rails/ActionView/Helpers/CaptureHelper/capture "concat api":http://apidock.com/rails/ActionView/Helpers/TextHelper/concat h2. layout中指定多个yield "content_for":http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for