Faker: Faker::Types.hash shadows Ruby core Object.hash method

Created on 16 Jan 2018  路  3Comments  路  Source: faker-ruby/faker

This shadowing breaks the ability to use Faker::Types as a hash key. While not something you would normally use a hash key, loggers and memory profilers are two use cases where this problem could creep in. This issue bit us in our rspec tests and was not a fun one to track down.

Object.hash:
"Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash.

The hash value is used along with eql? by the Hash class to determine if two objects reference the same hash key. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used." -https://ruby-doc.org/core-2.2.0/Object.html#method-i-hash

my_hash = {}
=> {}

hash[Object] = true
=> true

hash[Bignum] = true
=> true

hash[Faker::Types] = true
=> TypeError: no implicit conversion of String into Integer

BuFixes

Most helpful comment

because of this, the padrino framework fails to boot

All 3 comments

because of this, the padrino framework fails to boot

I see two possible fixes, which I don't mind implementing:

  • change the name of the hash method on Faker::Types to something like rb_hash (doing the same for other methods, such as array, for consistency)
  • stop requiring all fakers by default. I'm thinking we could have require faker/all for this, and then require faker/cat for a specific faker, which automatically gets the necessary base classes (Config and Base, I guess).

Since the first implementation (changing method names) was already merged, I guess we could close this issue, @vbrazo, @mkeiser82 ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neutralino1 picture neutralino1  路  3Comments

crispinheneise picture crispinheneise  路  6Comments

huyderman picture huyderman  路  4Comments

ggrillone picture ggrillone  路  6Comments

kevgathuku picture kevgathuku  路  4Comments