-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
| 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
~ end
end
Or define it as a class method:
class Discounts
~ # Handling of variables left out as exercise for the reader
~ def self.for
~ case[…]
~ end
end
That way you could call, for example:
Discounts.for vet
Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com
~ - 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
iEUEARECAAYFAkgggrwACgkQbtAgaoJTgL+aQgCWMqrQV0LqpvMEdFCb1yCluEKg
bACgnmMR8XvQzfoYalnQiH9ra3teVL4=
=hs10
-----END PGP SIGNATURE-----