Check system endianness using Ruby
Hello folks,
today I’d like to share with you a simple way to check the endianness of your system using Ruby and it’s based on the “pack” method in Ruby’s Array; pack simply represents the content of an array in a binary sequence according to a directive given to it as an argument.
Using this method mades the endianness verification as simple as writing this snippet:
if [1].pack("I") == [1].pack("N") puts "BigEndian" else puts "LittleEndian" end
The snippet verifies if the array [1] represented using native endianness (the “I” directive) is equal to the same array represented using network (big endian) endianness ( the “N” directive).
If the result is true the examined system is big endian, otherwise it is a little endian one.
Enjoy!