2006-10-30

A implementation of delegate pattern (2)

关键字: ruby
Secondly,write a delegator in another way
1.Code a class extends DelegateClass,and set a parameter that you want to use
2.Implementing your method initialize ,and donot forget to write super method.
3.Implementing your method which match to a parameter before you passed
4.And now,let's code
require 'delegate'

class MyDelegator < DelegateClass(Array)
  def initialize(array)
    super(array)
  end
  def [](*n)
    val = super(*n)
    #Add your logic here
  end
end


and now ,when u add your logic at method [](*n),the class MyDelegator willdelegate method [] of a Array ,just like this

array = %w{22 33 33 44}
myDelegator = MyDelegator.new(array)
puts array[0..3]

It will be output the result that u changed in your logic.
评论
发表评论

您还没有登录,请登录后发表评论

默海飞瀑
搜索本博客
博客分类
最近加入圈子
最新评论
评论排行榜