怪癖串联(竟然那么快贴到我)- -| 回首页 | 2005年索引 | - -Formula of Lunch

Metadata in Ruby- -

                                      

# assume a scene: some user-names are invalid

# when derive a concrete User implementation class

# we wish simply annotate which names should be forbidden in this class

 

require "active_support/class_inheritable_attributes"

 

class User

  include ClassInheritableAttributes

  def initialize(name, age)

    invalid_names = self.class.get_invalid_names # invoke class method

    if(invalid_names != nil)

      invalid_names.each do |name_symbol|

        invalid_name = name_symbol.to_s

        if(invalid_name == name)

          raise "Cannot use this name : " + name

        end

      end

    end

    @name = name

    @age = age

  end

 

  class <<self

    # class method can be used to set up meta-data

    def invalid_names(*names)

       write_inheritable_array("invalid_names", names)

    end

   

    # and meta-data would be read via class method

    def get_invalid_names

      read_inheritable_attribute("invalid_names")

    end

  end

end

 

class UserImpl < User

  invalid_names :fuck, :damn  # meta-data passed in as Symbols

end

 

u1 = UserImpl.new("Gigix", 24)  # will success

begin

  u2 = UserImpl.new("fuck", 24) # will raise a RuntimeError

rescue RuntimeError => e

end

u3 = User.new("fuck", 24)       # will success, User class not forbid any name

- 作者: 透明 2005年08月29日, 星期一 11:22

Trackback

你可以使用这个链接引用该篇日志 http://publishblog.blogdriver.com/blog/tb.b?diaryID=943050

博客手拉手

回复

- 评论人:byqhisgj

Thu May 17 03:34:42 CST 2007  作者邮箱  作者Blog

gzxwxwmemy[url=http://zvore.yi.org/82177478.html]brazilian pussy[/url][url=http://xenggi.66ghz.com/46330167.html]gun games[/url][url=http://wxaxw.22web.net/34298878.html]tatoo designs[/url][url=http://www32.websamba.com/viora/94041272.html]model com[/url][url=http://uenvc.22web.net/16709694.html]football games[/url][url=http://ninfrb.yi.org/20352297.html]blades glory cam xvid mvs[/url]

- 评论人:pwvdfgcp

Thu May 17 01:07:36 CST 2007  作者邮箱  作者Blog

dnxwxwmebe[url=http://ipenxa.isgreat.org/89084666.html]dog book[/url][url=http://netfov.4000webs.com/78621112.html]breathe on me[/url][url=http://netfov.4000webs.com/22495140.html]inflation adjusted[/url][url=http://netfov.4000webs.com/93023258.html]portrait posing[/url][url=http://wecnoa.10001mb.com/19319241.html]britney paris[/url][url=http://wecnoa.10001mb.com/14221517.html]transformer[/url][url=http://wecnoa.10001mb.com/77785671.html]inflation pressure[/url][url=http://wecnoa.10001mb.com/50315263.html]movie lines[/url]

- 评论人:ueolgwaa

Thu Oct 12 05:22:36 CST 2006  作者邮箱  作者Blog

vkxwxwmeqq[url=http://www.koolpages.com/kkjlehl/index.html]推广技巧[/url][url=http://www.koolpages.com/kkjlehl/26198047.htm]网站建设策划方案[/url][url=http://www.koolpages.com/kkjlehl/47348973.htm]引擎排名[/url][url=http://www.koolpages.com/kkjlehl/00157846.htm]网站推广mail[/url][url=http://www.koolpages.com/kkjlehl/95187597.htm]网站推广工具下载[/url][url=http://www.powow.com/oxvhseh/index.html]网站推广的方法[/url][url=http://www.powow.com/oxvhseh/80303016.htm]网站推广群发软件[/url][url=http://www.powow.com/oxvhseh/63576814.htm]网站排名榜[/url][url=http://www.powow.com/oxvhseh/46504715.htm]电子邮件群发[/url][url=http://www.powow.com/oxvhseh/90790388.htm]qq无限群发器[/url][url=http://www.pagerealm.com/qoitojq/index.html]如何建立网站[/url][url=http://www.pagerealm.com/qoitojq/40480876.htm]什么网站赚钱[/url][url=http://www.pagerealm.com/qoitojq/93765714.htm]qq消息群发工具[/url][url=http://www.pagerealm.com/qoitojq/13671137.htm]论坛自动发帖工具[/url][url=http://www.pagerealm.com/qoitojq/45301112.htm]进出口企业名录[/url]

- 评论人:w da

Tue Aug 30 06:28:48 CST 2005  作者邮箱  作者Blog

wer e

- 评论人:daquan

Mon Aug 29 19:35:15 CST 2005  作者Blog

sorry,没看清意图就评论了,原来是禁止非法名字的:)

- 评论人:daquan

Mon Aug 29 19:32:19 CST 2005  作者Blog

代码里面也不该有脏字儿吧

评论内容: