# Bignum problems

**URL:** <https://www.ruby-forum.com/t/bignum-problems/77184>\
**Category:** Ruby\
**Created:** [March 6, 2006, 6:41am UTC](https://www.ruby-forum.com/t/bignum-problems/77184 "2006-03-06T06:41:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 6:41am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/1 "2006-03-06T06:41:25Z")

</div>

The problems with Bignums in recent YAMLs have been discussed(\*), but  
does anyone have a patch that can be loaded in ruby code to get around  
it?

This just turned into a minor disaster for me…

$ ruby -v  
ruby 1.8.4 (2005-12-24) [i686-linux]  
$ ruby -r yaml -e ‘y 1234567890’  
— !ruby/object:Bignum 1234567890  
$ ruby -r yaml -e ‘YAML.load 1234567890.to\_yaml’  
/usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer': allocator undefined for Bignum (TypeError) from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load’  
from -e:1

(\*)  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2)

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 7:08am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/2 "2006-03-06T07:08:35Z")

</div>

Joel VanderWerf wrote:

> /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer': allocator undefined for Bignum (TypeError) from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load’  
> from -e:1
> 
> (\*)  
> [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2)

Answering my own question, but it’s a hack…

class Bignum  
def to\_yaml( opts = {} )  
super.sub(/!ruby/object:Bignum /, “”)  
end  
end

s = 1000000000000000.to\_yaml  
puts s  
p YAML.load(s)

OUTPUT:

— 1000000000000000  
1000000000000000

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 7:35am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/3 "2006-03-06T07:35:42Z")

</div>

Hello,

The latest ruby cvs seems to work ok?

bash-3.00# ruby -r yaml -e ‘y 1234567890000000000000’  
— 1234567890000000000000  
bash-3.00# ruby -r yaml -e ‘YAML.load 1234567890000000000000.to\_yaml’  
bash-3.00# ruby -v  
ruby 1.8.4 (2006-03-04) [i386-solaris2.10]

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 7:41am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/4 "2006-03-06T07:41:46Z")

</div>

Ville Mattila wrote:

> Hello,
> 
> The latest ruby cvs seems to work ok?
> 
> bash-3.00# ruby -r yaml -e ‘y 1234567890000000000000’  
> — 1234567890000000000000  
> bash-3.00# ruby -r yaml -e ‘YAML.load 1234567890000000000000.to\_yaml’  
> bash-3.00# ruby -v  
> ruby 1.8.4 (2006-03-04) [i386-solaris2.10]

That’s good to hear. I’d rather not have to install from cvs in all my  
locations, but my workaround didn’t work (see below), so maybe that’s  
necessary.

$ ruby  
require ‘yaml’

class Bignum  
def to\_yaml( opts = {} )  
super.sub(/!ruby/object:Bignum /, “”)  
end  
end

h={}  
h[1] = 1000000000000000  
y h

-:5:in `to_yaml': private method`sub’ called for  
#YAML::Syck::Scalar:0xb7b6d03c (NoMethodError)  
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:41:in `to_yaml' from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:40:in`to\_yaml’  
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:39:in `to_yaml' from /usr/local/lib/ruby/1.8/yaml.rb:387:in`quick\_emit’  
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:38:in `to_yaml' from /usr/local/lib/ruby/1.8/yaml.rb:117:in`dump’  
from /usr/local/lib/ruby/1.8/yaml.rb:428:in `y’  
from -:11

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 7:50am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/5 "2006-03-06T07:50:50Z")

</div>

Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> h={}  
> h[1] = 1000000000000000  
> y h

This works fine with CVS ruby, without your special hack.  
So probably the cvs is way to go.

## irb(main):001:0\> require ‘yaml’ =\> true irb(main):002:0\> irb(main):003:0\* h={} =\> {} irb(main):004:0\> h[1] = 1000000000000000 =\> 1000000000000000 irb(main):005:0\> y h

1: 1000000000000000  
=\> nil

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/bignum-problems/77184/6 "2006-03-06T12:56:35Z")

</div>

Joel,

If it’s helpfuly, here’s the small patch we’ve been using in Gentoo  
that I believe fixes this:

— ext/syck/rubyext.c 27 Sep 2005 22:57:52 -0000 1.30.2.15  
+++ ext/syck/rubyext.c 5 Oct 2005 10:24:16 -0000  
@@ -1142,6 +1142,9 @@  
}  
else if ( !NIL\_P( target\_class ) )  
{

- 

```
           if (subclass == rb_cBignum)

```

- 

```
               obj = rb_str2inum(val, 10);

```

- 

```
           else
            obj = rb_obj_alloc( subclass );
            if ( rb_respond_to( obj, s_yaml_initialize ) )
            {
```

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 6, 2006, 8:08am UTC](https://www.ruby-forum.com/t/bignum-problems/77184/7 "2006-03-06T08:08:58Z")

</div>

Joel VanderWerf wrote:

> /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer': allocator undefined for Bignum (TypeError) from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load’  
> from -e:1
> 
> (\*)  
> [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/4d62fa19fc7f4ffe/2331cd65b83bffe2?q=YAML+Bignum&rnum=1#2331cd65b83bffe2)

I think this workaround actually works:

if RUBY\_VERSION == “1.8.4”  
class Bignum  
def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out.scalar( nil, to\_s, :plain )  
}  
end  
end  
end

…that sure beats compiling cvs for several platforms and distributing  
it to users.
