Detect if the computer is little or big endian

Hi!

How can I know if my computer is little or big endian with Ruby?
I've searched the web but only got a few C/C++ solutions

Patrik Maunus wrote:

Hi!

How can I know if my computer is little or big endian with Ruby?
I've searched the web but only got a few C/C++ solutions

Perhaps pack a string into an integer and look at the result?

Windows XPPro, Intel

irb(main):002:0> [1,0,0,0].pack("i")
=> "\001\000\000\000"
irb(main):003:0>

Macintosh OS X, Power PC

irb(main):004:0> [1,0,0,0].pack("i")
=> "\000\000\000\001"
irb(main):005:0>

How can I know if my computer is little or big endian with Ruby?

You can test if [1].pack("I") == [1].pack("N")

rex% ruby -rrbconfig -e 'p Config::CONFIG["host"] if [1].pack("N") == [1].pack("I")'
"sparc-sun-solaris2.7"
rex%

moulon% ruby -rrbconfig -e 'p Config::CONFIG["host"] unless [1].pack("N") == [1].pack("I")'
"i686-pc-linux-gnu"
moulon%

Guy Decoux

I like this, thanks.

ยทยทยท

On 03/11/05, Peter Hickman <peter@semantico.com> wrote:

Windows XPPro, Intel

irb(main):002:0> [1,0,0,0].pack("i")
=> "\001\000\000\000"
irb(main):003:0>

Macintosh OS X, Power PC

irb(main):004:0> [1,0,0,0].pack("i")
=> "\000\000\000\001"
irb(main):005:0>