Rails每周闲碎(十一): Others

1. returning

returning String.new do |item|
     item << user.first_name
     item << user.second_name if user.second_name
     item << user.family_name
 end
 

     看看这个方法的定义:

 

def returning(value)
    yield(value)
    value
end

 

2. delegate

 

 

>> Person = Struct.new(:name, :address)

=> Person

>> class Invoice < Struct.new(:client)

>>   delegate :name, :address, :to => :client

>> end

=> [:name, :address]

>> john_doe = Person.new("John Doe", "Vimmersvej 13")

=> #<struct Person name="John Doe", address="Vimmersvej 13">

>> invoice = Invoice.new(john_doe)

=> #<struct Invoice client=#<struct Person name="John Doe", address="Vimmersvej 13">>

>> invoice.name

=> John Doe

>> invoice.address

=>Vimmersvej 13

 

 

  实现:

 

 

class Module

  # Delegate method 

  # It expects an array of arguments that contains 

  # the methods to be delegated and a hash of options

  def delegate(*methods)

    # Pop up the options hash from arguments array

    options = methods.pop

    # Check the availability of the options hash

    # and more specifically the :to option

    # Raises an error if one of them is not there

    unless options.is_a?(Hash) && to = options[:to]

      raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."

    end


    # Make sure the :to option follows syntax rules

    # for method names 

    if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/

      raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."

    end


    # Set the real prefix value 

    prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"


   # Here comes the magic of ruby :) 

   # Reflection techniques are used here:

   # module_eval is used to add new methods on the fly which:

   # expose the contained methods' objects

    methods.each do |method|

      module_eval("def #{prefix}#{method}(*args, &block)\n#{to}.__send__(#{method.inspect}, *args, &block)\nend\n", "(__DELEGATION__)", 1)

    end

  end

end
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值