作者godfat (godfat 真常)
看板Ruby
标题Re: [问题] Ruby 与 Interface Oriented Design
时间Wed Feb 14 15:48:44 2007
※ 引述《yzugsr (Bird)》之铭言:
: 一点浅见 请指教
: 在ruby里, interface的观念应该直接用duck typing的观念实作
: ==别挑我语法错, 下面都是pesudo code==
怕新手误会,所以我还是点一下语法和用法的错误… XD
: Ruby:
: class Dog
: def move, puts "Dogs walk"; end; end
def move
; puts "Dogs walk"; end; end
: class Bird
: def move, puts "Birds fly"; end; end
def move
; puts "Birds fly"; end; end
: interface呢....以上我讲完等於没讲一样
: 所以老师说 ruby一定要写test case (丢笔)
: (其实是这句书上说的....)
: 在test case里确认你认为每个是animal的class, 都implement了animal该有的method
: 这样做等於你把interface定义在test case里, 修改程式後记得跑test确认他是对的
yes, 差不多就是这个概念
static typing 的做法是把 spec 写在你的核心程式中
duck typing 的做法则是把 spec 写在 test case 里
这也是一种 test driven 的做法
: class TC_Bird < Test::Unit::TestCase
: test_acts_as_animal(Bird)
: end
刚刚试了一下,TestCase 好像不能这样用
一定要 def test开头的methd; end
: module AnimalTestHelper
: def test_acts_as_animal(the_class)
: obj = the_class.new
: obj.move
: end
: end
把上面这个拉到 TC_Bird 的上面,然後 TC_Bird 改写成:
class TC_Bird < Test::Unit::TestCase
include AnimalTestHelper
def test_01
test_acts_as_animal Bird
test_acts_as_animal Dog
end
end
: class Test::Unit::TestCase, include AnimalTestHelper; end
不要这样做,太暴力了… XD
, 也该改成 ; 否则语法错误
--
#!/usr/bin/ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》
# if a
dog nailed
extra legs that
http://webptt.com/cn.aspx?n=bbs/Ruby/index.html
#
walks like an octopus, and Welcome ~
Ruby@ptt~
#
talks like an octopus, then
◢█◣ http://www.ruby-lang.org/
# we are happy to treat it as
█ http://www.ruby-doc.org/
# if it were
an octopus.
◥ ◤ http://www.rubyforge.org/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.135.28.18
1F:推 yzugsr:嗯我知道不能这样用 我只是写pesudo code 还是谢谢你 ^^ 02/14 17:12