Ruby中delegate的一些用法_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Ruby中delegate的一些用法

Ruby中delegate的一些用法

 2011/8/10 17:30:52  inosin  http://inosin.iteye.com  我要评论(0)
  • 摘要:ClassAccount<ActiveRecord::Basedelegate:firstname,:to=>:user#firstname=>user.firstnamedelegate:firstname,:to=>:user,:prefix=>true#user_firstname=>user.firstnamedelegate:firstname,:to=>:user,:allow_nil=>true,:prefix=>
  • 标签:用法 Ruby
Class Account < ActiveRecord::Base
 
    delegate :firstname, :to => :user
    # firstname => user.firstname

    delegate :firstname, :to => :user, :prefix => true
    # user_firstname => user.firstname

    delegate :firstname, :to => :user, :allow_nil => true, :prefix => true
    # user_firstname => user.try(:firstname)
 
    delegate :name, :to => "user.try(:editor)", :prefix => :editor
    # editor_name => user.editor.name

    delegate :name, :to => "user.try(:editor)", :prefix => :editor, :allow_nil => true
    # editor_name => user.editor.try(:name)
 
end

参考:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb

发表评论
用户名: 匿名