Confused on testing a Class as a Key in a Hash

Actual code: https://github.com/LeamHall/CT_Character_Generator/blob/master/test/tc_Citizen.rb

Trying to figure out why I can't compare a Hash key to it's value. The key is a Class but if I use hash.each_key or each_pair it shows the key and value. If I try to assert that hash[key] == value, it fails.

Besides a brain, a clue, and a life, what am I missing?

Thanks!

Leam

I wasn't able to run the test right from the git clone, or I'm running
the tests in a wrong way or the load path seems to be wrong. Anyway,
it looks like the Career class is updating the character with a class
as the key:

  def update_character(character, terms)
    this_career = self.class
    character.careers[this_career] = terms

but in the test:

@character.careers["Citizen"] == 2

If I read the code correctly that should be
@character.careers[Citizen.class] == 2

Hope this helps,

Jesus.

···

On Mon, Apr 17, 2017 at 12:27 PM, Leam Hall <leamhall@gmail.com> wrote:

Actual code:
https://github.com/LeamHall/CT_Character_Generator/blob/master/test/tc_Citizen.rb

Trying to figure out why I can't compare a Hash key to it's value. The key
is a Class but if I use hash.each_key or each_pair it shows the key and
value. If I try to assert that hash[key] == value, it fails.

Besides a brain, a clue, and a life, what am I missing?

Actual code:
https://github.com/LeamHall/CT_Character_Generator/blob/

master/test/tc_Citizen.rb

Trying to figure out why I can't compare a Hash key to it's value. The key
is a Class but if I use hash.each_key or each_pair it shows the key and
value. If I try to assert that hash[key] == value, it fails.

Besides a brain, a clue, and a life, what am I missing?

I wasn't able to run the test right from the git clone, or I'm running
the tests in a wrong way or the load path seems to be wrong. Anyway,
it looks like the Career class is updating the character with a class
as the key:

  def update_character(character, terms)
    this_career = self.class
    character.careers[this_career] = terms

but in the test:

@character.careers["Citizen"] == 2

If I read the code correctly that should be
@character.careers[Citizen.class] == 2

Hope this helps,

Jesus.

Isn't `Citizen.class == Class` ?

If so, it should probably be:

    @character.careers[Citizen] == 2

Cheers

···

On 18 Apr. 2017 01:10, "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote:
On Mon, Apr 17, 2017 at 12:27 PM, Leam Hall <leamhall@gmail.com> wrote:

That works, and it suggests that my earlier efforts at careers.key? failed due to using the string "Citizen" vice the class.

Thanks!

Leam

···

On 04/17/17 16:48, Matthew Kerwin wrote:

On 18 Apr. 2017 01:10, "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com > <mailto:jgabrielygalan@gmail.com>> wrote:

    On Mon, Apr 17, 2017 at 12:27 PM, Leam Hall <leamhall@gmail.com > <mailto:leamhall@gmail.com>> wrote:
    > Actual code:
    >
    https://github.com/LeamHall/CT_Character_Generator/blob/master/test/tc_Citizen.rb
    <https://github.com/LeamHall/CT_Character_Generator/blob/master/test/tc_Citizen.rb&gt;
    >
    > Trying to figure out why I can't compare a Hash key to it's value.
    The key
    > is a Class but if I use hash.each_key or each_pair it shows the
    key and
    > value. If I try to assert that hash[key] == value, it fails.
    >
    > Besides a brain, a clue, and a life, what am I missing?

    I wasn't able to run the test right from the git clone, or I'm running
    the tests in a wrong way or the load path seems to be wrong. Anyway,
    it looks like the Career class is updating the character with a class
    as the key:

      def update_character(character, terms)
        this_career = self.class
        character.careers[this_career] = terms

    but in the test:

    @character.careers["Citizen"] == 2

    If I read the code correctly that should be
    @character.careers[Citizen.class] == 2

    Hope this helps,

    Jesus.

Isn't `Citizen.class == Class` ?

If so, it should probably be:

    @character.careers[Citizen] == 2

Cheers

So, had to go do some server work in the garage. Interestingly the test now passes but I'm going to have to refactor. The careers hash wasn't supposed to have a class as a key!

Leam

···

On 04/17/17 16:48, Matthew Kerwin wrote:

Isn't `Citizen.class == Class` ?

If so, it should probably be:

    @character.careers[Citizen] == 2

Cheers

​If you want something a bit more vanilla, remember that `SomeClass.name =>
"SomeClass"`

But yeah, the whole thing smells a bit like something got confused at some
point.

Cheers

···

On 18 April 2017 at 09:50, Leam Hall <leamhall@gmail.com> wrote:

On 04/17/17 16:48, Matthew Kerwin wrote:

Isn't `Citizen.class == Class` ?

If so, it should probably be:

    @character.careers[Citizen] == 2

Cheers

So, had to go do some server work in the garage. Interestingly the test
now passes but I'm going to have to refactor. The careers hash wasn't
supposed to have a class as a key!

Leam

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Oooops, sorry, you are right. Too much Java lately...

Jesus.

···

On Mon, Apr 17, 2017 at 10:48 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Isn't `Citizen.class == Class` ?

If so, it should probably be:

@character.careers[Citizen] == 2