Best way to handle Integer/Bignum/Fixnum

Hello,

In my code, after migrating to 2.4.0, I see a lot of

gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Fixnum is deprecated
gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Bignum is deprecated

This code still works okay, but I was wondering, how should I write my
code now to not produce warnings with both 2.3 and 2.4?

e.g. the code trivially could be something like this:

if RUBY_VERSION < "2.4"
    IntegralTypes = [Bignum, Fixnum]
else
    IntegralTypes = [Integer]
end

But this seems very ugly.

It's not only my gem this is an issue, it's also present in
active_support/xml_mini.rb

Thanks for any ideas.

Kind regards,
Samuel

Any reason your base code can't use Integer alone? If I understand correctly, Ruby will convert a very large number to Bignum if necessary, in older Ruby versions. Take that as from a newbie, and verify. :slight_smile:

Leam

···

On 02/20/17 04:33, Samuel Williams wrote:

Hello,

In my code, after migrating to 2.4.0, I see a lot of

gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Fixnum is deprecated
gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Bignum is deprecated

This code still works okay, but I was wondering, how should I write my
code now to not produce warnings with both 2.3 and 2.4?

e.g. the code trivially could be something like this:

if RUBY_VERSION < "2.4"
    IntegralTypes = [Bignum, Fixnum]
else
    IntegralTypes = [Integer]
end

But this seems very ugly.

It's not only my gem this is an issue, it's also present in
active_support/xml_mini.rb

Thanks for any ideas.

Kind regards,
Samuel

How do you use IntegralTypes in your code?
The "IntegralTypes = [Integer]" should work for <2.4 if you
use .kind_of? instead of exact matching via ".class =="

I normally use one of the following, all of which work in all
Ruby versions:

a) if Integer === foo
      ...

b) if foo.kind_of?(Integer)
      ...

c) case foo
    when Integer
       ...

···

Samuel Williams <space.ship.traveller@gmail.com> wrote:

Hello,

In my code, after migrating to 2.4.0, I see a lot of

gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Fixnum is deprecated
gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Bignum is deprecated

This code still works okay, but I was wondering, how should I write my
code now to not produce warnings with both 2.3 and 2.4?

e.g. the code trivially could be something like this:

if RUBY_VERSION < "2.4"
    IntegralTypes = [Bignum, Fixnum]
else
    IntegralTypes = [Integer]
end

But this seems very ugly.

If you use the name of the class in any way... Your suggestion gave me
the idea to check how xml_mini.rb works.. now they "fixed" it.

It's a git ugly, is that the best we can do?

···

On 20 February 2017 at 23:40, Eric Wong <e@80x24.org> wrote:

Samuel Williams <space.ship.traveller@gmail.com> wrote:

Hello,

In my code, after migrating to 2.4.0, I see a lot of

gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Fixnum is deprecated
gems/mapping-1.0.0/lib/mapping/object_model.rb:26: warning: constant
::Bignum is deprecated

This code still works okay, but I was wondering, how should I write my
code now to not produce warnings with both 2.3 and 2.4?

e.g. the code trivially could be something like this:

if RUBY_VERSION < "2.4"
    IntegralTypes = [Bignum, Fixnum]
else
    IntegralTypes = [Integer]
end

But this seems very ugly.

How do you use IntegralTypes in your code?
The "IntegralTypes = [Integer]" should work for <2.4 if you
use .kind_of? instead of exact matching via ".class =="

I normally use one of the following, all of which work in all
Ruby versions:

a) if Integer === foo
      ...

b) if foo.kind_of?(Integer)
      ...

c) case foo
    when Integer
       ...

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

> a) if Integer === foo
> ...
>
> b) if foo.kind_of?(Integer)
> ...
>
> c) case foo
> when Integer
> ...
>

<top-posting corrected>

If you use the name of the class in any way... Your suggestion gave me
the idea to check how xml_mini.rb works.. now they "fixed" it.

https://github.com/rails/rails/blob/71da39097b67114329be6d8db7fe6911124531af/activesupport/lib/active_support/xml_mini.rb#L35-L53

It's a git ugly, is that the best we can do?

Unless TYPE_NAMES hash is used elsewhere, I don't think they
need to be using that hash... They can use the case/when I
described in c) above.

Calling foo.class.name also generates short-lived garbage
strings if that code path is called frequently. Checking
#kind_of? and === (via case/when) won't make garbage.

Anyways I don't use Rails or centralized/proprietary messaging
systems; but maybe somebody who does can rework the code to
be version-independent and generate less garbage at the same
time...

···

Samuel Williams <space.ship.traveller@gmail.com> wrote:

On 20 February 2017 at 23:40, Eric Wong <e@80x24.org> wrote: