作者ihower (好2ˇ)
看板Ruby
标题Re: [问题] 相同的instance和class method
时间Mon May 10 17:21:58 2010
是指 instance method 跟 class method 一样吗? XD
def price
self.class.price
end
def self.price
...
end
※ 引述《bypang (甚麽时候才等到你)》之铭言:
: 如果有相同结构的instance和class method,要如何做才能dry呢?
: class Product < ActiveRecord::Base
: def price
: ...
: end
: def self.price
: ...
: end
: end
: class ProductsController < ApplicationController
: # use in ajax call
: def calculate
: price = Product.price
: end
: end
: 谢谢各位大大
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.43.194.211
1F:推 bypang:谢谢,真神奇,可以了 05/10 18:30
2F:推 godfat:不建议这样设计,很混淆 05/10 22:01
3F:推 bypang:godfat大大有何建议吗?一个是virtual attributes 05/11 12:42
4F:推 bypang:一个是class method,可是都有相同的code 05/11 12:45
5F:→ godfat:看起来不像是 virtual attribute,而是class method wrapper 05/11 15:17