自动化测试之路(三) ruby里的get与set方法
照例可以先看端程序
class Person
def initialize( name,age=18 )
@name = name
@age = age
@motherland = "China"
end
def talk
puts "my name is "+@name+", age is "+@age.to_s
if @motherland == "China"
puts "I\'m Chinese."
else
puts "I\'m foreigner."
end
end
attr_writer :motherland
end
p1=Person.new("kaichuan",20)
p1.talk
p2=Person.new("Ben")
p2.motherland="ABC"
p2.talk
程序运行结果
my name is kaichuan, age is 20
I'm Chinese.
my name is Ben, age is 18
I'm foreigner.
>Exit code: 0
有过面向对象编程语言经历的童鞋很容易可以看明白,但是里面值的一提的是这句:“attr_writer :motherland”
这句话相当于我们面向对象编程语言的set方法,也可以这样写
def motherland=(value)
return @motherland =value
end
那么相应的get方法为 attr_ reader :motherland
相当于
def motherland
return @motherland
end
相应的 attr_accessor :motherland 相当于attr_reader:motherland; attr_writer :motherland
相关文档:
最近在看John E.Hopcroft,Rajeev Motwani,Jeffrey D.Ullman 三巨头写的Introduction to Automata Theory,Language,and Computation,想写一个Turing 机验证一下自己写的状态转移函数对不对。懒得很,网上搜了几个不错的。但Ruby Quiz 上的这个最简单。
162 Turing 机
问题描述
Quiz
description by James Edward Gray ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 680460288 22 0 262145 0;}
@font-face
{font-family:"Cambria Mat ......
松本行宏如约于圣诞节发布了Ruby 1.9。根据Ruby的惯例,小数点后面第一位如果是单数,那么就表明这是一个实验版本,不推荐用于产品环境。所谓“产品环境”,对于目前的Ruby来说,基本上就是Ruby on Rails。从目前RoR社群的反映来看,确实有人正在尝试用Ruby 1.9配合RoR,但是尚属 ......
ruby中自带实现观察者模式的类observer。可以利用它来实现观察者模式。
代码例子:
# -*- coding: GB2312 -*-
require 'observer'
# 观察者模式(ruby)的使用例子
# 被观察者P
class PObservable
include Observable
end
# 观察者A
class AObserver
# update方法名是必须的要有的
def update(arg)
puts "AO ......