Hi Garrow,

Thanks for the idea. I didn't mention it, but the Currency objects also stored the Exchanges that they were traded at.

I've now planning a GlobalMarket with a Hash mapping from Currency to Array of Exchange.

It was an interesting road to get here. Reflecting upon and abstracting the process I used to remove the class variables, I asked myself:

What functions does the singleton object (the Currency class itself) provide?
How can they be mapped to a singleton class?

Tom

On 2/01/2016 3:15 PM, Garrow Bedrossian wrote:
Hi Tom

If you are trying to only keep one instance of a particular currency
around •just• for the sake of comparisons, you can remove that need by
treating currency as a value object.

You can define the ==(other) method on your class, and then simple
comparisons of a.currency == b.currency will be true, even when they are
different instances of the same class.

def ==(other)
   if other.class == self.class
     other.currency_code == self.currency_code
   else
     super
   end
end

(Excuse bad formatting)

Class Variables are fraught with gotchas, especially in tests and hot
code reloading. I've had issues getting stale data on production deploys
with code that uses
Class vars. :(

On Saturday, 2 January 2016, Gareth Townsend <gareth.towns...@me.com
<mailto:gareth.towns...@me.com>> wrote:

    Hi Tom,

    Are you using the Money gem? If not, I suggest you check it out.

    Gareth Townsend
    http://www.garethtownsend.info

    > On 2 Jan 2016, at 19:27, Ravi (Tom) Hale <r...@hale.ee <javascript:;>> 
wrote:
    >
    > Hi all,
    >
    > Me again :) Simon Hildebrandt recommended me to join here as I've been 
largely learning / using Ruby in a vacuum (I'm teaching myself while backpacking 
planet Earth). It's great to have people to ask these questions - and well 
thought-out and clearly written replies coming back.
    >
    > I'm working on a program that deals with currency trading. The Currency 
class keeps track of all instances so that only one instance of (for example) :AUD is 
created. It does this via `private_class_method :new` and a class instance Hash of 
(currency_code => instance) to store already created objects.
    >
    > I'm trying to work out how to test this class without the tests being 
order-dependent.
    >
    > I've currently got a spec with:
    > context 'before the first instance is created'
    >
    > This was working just fine until I added some more tests in front of it, 
where currency objects were created.
    >
    > I see a few options:
    > 1) Create a Currency.reset! class method to reset the class to the state 
at BEGIN
    > 2) Somehow reset the Currency class to load time in a more elegant way
    > 3) Get rid of the class variables entirely (and still ensure that only 
object per currency code exists)
    >
    > I'm fudging with 1) for now to keep in the Green. 3 seems to be cleanest 
in terms of textbook OO design, but I can't see how to textbookify this...
    >
    > Any suggestions on how to implement 2, 3 (or 4..)?
    >
    > Cheers,
    > Tom
    >
    > P.S. Let me know if there is a more appropriate forum to ask questions 
such as this
    >
    > --
    > You received this message because you are subscribed to the Google Groups 
"Ruby or Rails Oceania" group.
    > To unsubscribe from this group and stop receiving emails from it, send an email 
torails-oceania+unsubscr...@googlegroups.com <javascript:;>.
    > To post to this group, send email torails-ocea...@googlegroups.com 
<javascript:;>.
    > Visit this group athttps://groups.google.com/group/rails-oceania.
    > For more options, visithttps://groups.google.com/d/optout.

    --
    You received this message because you are subscribed to the Google
    Groups "Ruby or Rails Oceania" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to rails-oceania+unsubscr...@googlegroups.com
    <javascript:;>.
    To post to this group, send email to rails-oceania@googlegroups.com
    <javascript:;>.
    Visit this group at https://groups.google.com/group/rails-oceania.
    For more options, visit https://groups.google.com/d/optout.



--
Garrow Bedrossian
+ 61 401 532 538

--
You received this message because you are subscribed to the Google
Groups "Ruby or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to rails-oceania+unsubscr...@googlegroups.com
<mailto:rails-oceania+unsubscr...@googlegroups.com>.
To post to this group, send email to rails-oceania@googlegroups.com
<mailto:rails-oceania@googlegroups.com>.
Visit this group at https://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby or 
Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rails-oceania+unsubscr...@googlegroups.com.
To post to this group, send email to rails-oceania@googlegroups.com.
Visit this group at https://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.

Reply via email to