TianShuai ′s Blog

Record my bit by bit...

Ruby之面向对象

| Comments

面向对象的三个基本特征是:封装、继承、多态。

o_OOBase.gif

封装

封装,保证对象自身数据的完整性、安全性

继承

建立类之间的关系,实现代码复用、方便系统的扩展

  • 继承概念的实现方式有三类:实现继承、接口继承和可视继承。
    Ø 实现继承是指使用基类的属性和方法而无需额外编码的能力;
    Ø 接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力;
    Ø 可视继承是指子窗体(类)使用基窗体(类)的外观和实现代码的能力。
  • 在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是“属于”关系。例如,Employee 是一个人,Manager 也是一个人,因此这两个类都可以继承 Person 类。但是 Leg 类却不能继承 Person 类,因为腿并不是一个人。
  • 抽象类仅定义将由子类创建的一般属性和方法,创建抽象类时,请使用关键字 Interface 而不是 Class。
  • OO开发范式大致为:划分对象→抽象类→将类组织成为层次化结构(继承和合成) →用类与实例进行设计和实现几个阶段。

多态

相同的方法调用可实现不同的实现方式

  • 实现多态,有二种方式,覆盖,重载。
  • 覆盖,是指子类重新定义父类的虚函数的做法。
  • 重载,是指允许存在多个同名函数,而这些函数的参数表不同(或许参数个数不同,或许参数类型不同,或许两者都不同)。

G++: Internal Error: Killed (Program Cc1plus)

| Comments

买了阿里云服务,系统为centos 6.3,内存512M.通过passenger安装nginx: rvmsudo Passenger-install-nginx-module 在编译nginx时总是报错:g++: Internal error: Killed (program cc1plus)google很多终于找到问题:http://www.linlook.com/index/article/articleid/1350/ 以下引自原处:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
内存不足, 在linux下增加临时swap空间
step 1:
  #dd if=/dev/zero of=/home/swap bs=1024 count=500000
  注释:of=/home/swap,放置swap的空间; count的大小就是增加的swap空间的大小,1024就是块大小,这里是1K,所以总共空间就是bs*count=500M
step 2:
  # mkswap /home/swap
  注释:把刚才空间格式化成swap各式
step 3:
  #swapon /home/swap
  注释:使刚才创建的swap空间

      如果想关闭刚开辟的swap空间,只需命令:#swapoff

 
如上操作以后,你的安装过程就少了这道阻拦拉拉拉!!! 当然对于公司工作估计很难遇到此种情况啦啦,你们配置都很牛逼滴!

我收藏的网址---实时更新

| Comments

在线js测试: http://www.ostools.net/jsbin#source

在线Markdown测试: http://johnmacfarlane.net/pandoc/try/

rails插件集合: http://blog.163.com/prevBlogPerma.do?host=ebenz&srl=14492363520114145023611&mode=prev

blogs.ejb.cc http://blogs.ejb.cc

候西阳的博客: http://houxiyang.com/archives/68/

李骥平技术博客: http://fsjoy.blog.51cto.com/318484/d-3

剑诩的学习笔记: http://phsblog.sinaapp.com/index.php/Index/index

用Sinatra编写博客应用: http://kb.cnblogs.com/page/89619/

sinatra中文站: http://www.sinatrarb.com/intro-zh.html

Rails3 在线文档--实时更新

| Comments

  • 是否是开发环境?
1
if Rails.env.development?
  • 智能判断单复数
1
pluralize(count, "error")
  • 上传文件名的话,用 userid + fileid + timestamp 取 sha1 就能解决唯一性了

  • 把数字转换为字节(用于精确判断文件或图片大小)

1
2
3
4
1.kilobyte #千字节
#=>1024
5.megabytes  #上传文件限制字节数5M
#=> 5242880
  • 判断表单是新建还是修改的
1
2
3
4
User.new.new_record?
#=>true
User.first.new_record?
#=>false

Linux 系统命令大全

| Comments

系统信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
arch 显示机器的处理器架构(1) 
uname -m 显示机器的处理器架构(2) 
uname -r 显示正在使用的内核版本 
dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI) 
hdparm -i /dev/hda 罗列一个磁盘的架构特性 
hdparm -tT /dev/sda 在磁盘上执行测试性读取操作 
cat /proc/cpuinfo 显示CPU info的信息 
cat /proc/interrupts 显示中断 
cat /proc/meminfo 校验内存使用 
cat /proc/swaps 显示哪些swap被使用 
cat /proc/version 显示内核的版本 
cat /proc/net/dev 显示网络适配器及统计 
cat /proc/mounts 显示已加载的文件系统 
lspci -tv 罗列 PCI 设备 
lsusb -tv 显示 USB 设备 
date 显示系统日期 
cal 2007 显示2007年的日历表 
date 041217002007.00 设置日期和时间 - 月日时分年.秒 
clock -w 将时间修改保存到 BIOS 

关机 (系统的关机、重启以及登出 )

1
2
3
4
5
6
7
8
shutdown -h now 关闭系统(1) 
init 0 关闭系统(2) 
telinit 0 关闭系统(3) 
shutdown -h hours:minutes & 按预定时间关闭系统 
shutdown -c 取消按预定时间关闭系统 
shutdown -r now 重启(1) 
reboot 重启(2) 
logout 注销 

Ruby 进阶

| Comments

1.下载网页中图片

1
2
3
4
5
6
7
8
require 'net/http'
Net::HTTP.start("www.google.com.hk") { |http|
  resp = http.get("/images/srpr/nav_logo27.png")
  open("D:/test.png", "wb") { |file|
    file.write(resp.body)
   }
}
puts "OK"

Ruby 编程风格介绍

| Comments

学习一门新的语言时,要先养成好的编程风格,然后将这种风格应用到coding中,慢慢地成为习惯。

本页用于介绍 Ruby 社区首推的代码编写风格,本文翻译来自: https://github.com/bbatsov/ruby-style-guide 目录

格式,文件格式
语法
命名
注释
注解
类相关
异常
集合
字符串
正则表达式
百分号
元编程
杂项

The Ruby Style Guide

This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers. A style guide that reflects real-world usage gets used, and a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all – no matter how good it is.

The guide is separated into several sections of related rules. I’ve tried to add the rationale behind the rules (if it’s omitted I’ve assumed that is pretty obvious).

I didn’t come up with all the rules out of nowhere – they are mostly based on my extensive career as a professional software engineer, feedback and suggestions from members of the Ruby community and various highly regarded Ruby programming resources, such as “Programming Ruby 1.9” and “The Ruby Programming Language”.

The guide is still a work in progress – some rules are lacking examples, some rules don’t have examples that illustrate them clearly enough. In due time these issues will be addressed – just keep them in mind for now. 格式, 文件格式

Rails 风格指导

| Comments

感谢译者。

本页用于介绍 Ruby 社区首推的Rails代码编写风格,翻译来自: https://github.com/JuanitoFatas/rails-style-guide 序幕

风格是从伟大事物中分离出的美好事物。 
 Bozhidar Batsov

这份指南目的于演示一整套 Rails 3 开发的风格惯例及最佳实践。这是一份与由现存社群所驱动的Ruby 编码风格指南互补的指南。

而本指南中测试 Rails 应用小节摆在开发 Rails 应用之后,因为我相信行为驱动开发 (BDD) 是最佳的软体开发之道。铭记在心吧。

Rails 是一个坚持己见的框架,而这也是一份坚持己见的指南。在我的心里,我坚信 RSpec 优于 Test::Unit,Sass 优于 CSS 以及 Haml,(Slim) 优于 Erb. 所以不要期望在这里找到 Test::Unit, CSS 及 Erb 的忠告。

某些忠告仅适用于 Rails 3.1+ 以上版本。

Nokogiri抓取失败后重试代码

| Comments

非常喜欢Nokogiri的简洁,尤其是根据css和xpath查找元素。有时又觉得Nokogiri太简洁了,连抓取失败重试的机制都没有。可惜在景德镇,网络经常丢包,真是郁闷。 所以写这段代码,以后肯定常用。

1
2
3
4
5
6
begin
  doc = Nokogiri::HTML(open(url).read.strip)
rescue Exception => ex
  log.error "Error: #{ex}"
  retry
end

Tips: retry可以跳回begin 这段代码将打印log并一直重试直到成功。估计这样写不大合适,因为一旦发生一个小错误,将会导致死循环。比较好的做法是,循环10次,如果都失败就放弃。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#定义常量,最多循环10次
MAX_ATTEMPTS = 10

doc = nil
begin
  doc = Nokogiri::HTML(open(url).read.strip)
rescue Exception => ex
  log.error "Error: #{ex}"
  attempts = attempts + 1
  retry if(attempts < MAX_ATTEMPTS)
end

if(doc.nil?)
  # 尝试10次后都失败,在这里处理一下。
  # 以免后面处理doc时抛空指针异常
end

原文链接:http://rubyer.me/blog/537/