Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Include code (conditionally) in a class and its subclasses
Alfredo Garcia lopez
10/22/2008 1:19:00 PM
Hi.
Is there a way to include code in a class and all its subclasses and get
the behaviour as if it was included at the end of the code in each of
them?
Example:
class Product<ActiveRecord::Base
def price
100
end
end
module ProductExtension
def price
120
end
end
I would wish find a way to include this module to the
superclass(ActiveRecord::Base) and get the following result:
@product=Product.new
@product.price =>120
or even
module ProductExtension
def precio
old_price+30
end
alias_method :old_price, :price
alias_method :price, :old_price
end
@product=Product.new
@product.price =>130
The module to include would be conditional.
Ex: If the current class is Product I would include ProductExtension.
If the current class is Category I would include CategoryExtension.
Perhaps there´s a better way to chieve this.Accept suggestions.
Thanks for reading.
--
Posted via
http://www.ruby-...
.
1 Answer
Alfredo Garcia lopez
10/22/2008 1:23:00 PM
0
Sorry,this is wrong
>
> module ProductExtension
> def precio
> old_price+30
> end
> alias_method :old_price, :price
> alias_method :price, :old_price
> end
It would be ...
module ProductExtension
def new_price
old_price+30
end
alias_method :old_price, :price
alias_method :price, :new_price
end
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Include code (conditionally) in a class and its subclasses
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password