2016-07-15 38 views
0

我正在尝试将用户输入à“6个月”转换成一个Ruby方法àla 6.months如何在没有接收器的情况下使用Ruby的send方法?

这怎么办?

我不断收到undefined method 6.months错误行10

感谢您的帮助。

class Coupon < ActiveRecord::Base 

    def self.duration_options 
    ["3 years", "2 years", "1 year", "6 months", "3 months", "1 month"] 
    end 

    def end_at 
    if Coupon.duration_options.include?(duration) 
     method = duration.sub!(' ', '.') 
     time = Time.zone.now + send(method) 
    end 
    time 
    end 

end 

回答

2

send是对象本身上,在这种情况下,它self.send(method)

你可以做这样的

if Coupon.duration_options.include?(duration) 
    off_set = duration.split(" ") 
    time = Time.zone.now + off_set.first.to_i.send(off_set.last.to_sym) 
end 
time 
相关问题