-----BEGIN PGP SIGNED MESSAGE-----
Phillip G. wrote:
| So, let’s do a Classic version (pardon the pun):
| class Discounts
| ~ attr_reader :friend, :vet, :super_vet
| ~ def initialize
| ~ @friend, @vet, @super_vet = 40, 100, 160
| ~ end
| ~ def method_missing
| ~ 0 # The default ‘discount’
| ~ end
| discount_for = Discounts.new
| discount_for vet
| => 100
| discount_for somebody_else
| => 0
| Beware of bugs; I’ve only proved the code correct, not tested it.
Well, not even that bit of due diligence. sighs
Obviously, that doesn’t work.
discount_for.vet # that does work with the above code.
However, defining a singleton method would provide what I like to see.
def discounts_for buyer
~ case buyer
~ when friend : 40
~ else 0
Or define it as a class method:
~ # Handling of variables left out as exercise for the reader
~ def self.for
That way you could call, for example:
~ - You know you’ve been hacking too long when…
…you send E-mail and end each line with \n.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----