3

In Ruby script, I want to get OS information, not only Linux, but also 32bit or 64bit.

That's because my program will run on multiple Linux platform. It calls a third part tool, the tool has sub-folder: lin32, lin64, I need to call proper version based on OS info.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
TieDad
  • 9,143
  • 5
  • 32
  • 58
  • 3
    possible duplicate of [How can I find which operating system my Ruby program is running on?](http://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on) – Roman Kiselenko Sep 17 '15 at 11:45
  • @Зелёный: this is a very promising duplicate, however the answers included there does not cover the 'architecture' (x86/x64) part. If any actually tries to, they mostly tell about the compilation version of Ruby which is not relevant here (you can run 32bit ruby on 64bit os, and in this case OP needs to get '64' reponse) – quetzalcoatl Sep 17 '15 at 11:50
  • I wanted to point you at the documentation for `RbConfig`, but unfortunately, it appears to be completely undocumented. Nevertheless, using `RbConfig` is the correct answer, provided you can figure out *how* to use it. – Jörg W Mittag Sep 17 '15 at 13:54

4 Answers4

5

In Ruby you can use RUBY_PLATFORM constant. This constant produces a base name of your OS and kernel bits level. E.g. in irb:

1.9.3-p392 :001 > RUBY_PLATFORM
=> "x86_64-linux" - Linux based OS with 64-bit
=> "i686-linux" - Linux based OS with 32-bit
Alexander Shlenchack
  • 3,779
  • 6
  • 32
  • 46
2

Also, you can try someth like:

ver = `getconf LONG_BIT`

or

ver = `arch`
Oleksandr Holubenko
  • 4,310
  • 2
  • 14
  • 28
0

Since you refer to linux, use uname -m as described here: How to determine whether a given Linux is 32 bit or 64 bit?

ignore the bottom "CPU" part. because you want to run a program, you want the kernel architecture:

irb:(main):001:0> `uname -m`
=> "x86_64"

x86_64 ==> 64-bit kernel
i686   ==> 32-bit kernel
Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

You can use the RUBY_PLATFORM constant

irb(main):001:0> RUBY_PLATFORM
=> "i686-linux"
Arif
  • 1,369
  • 14
  • 39
  • this will not tell him what is the OS version, but will tell him in what mode Ruby was compiled. For OS=x86 there is no difference, but on OS=x64 you can run x86 Ruby and that snippet will return x86 instead of x64 – quetzalcoatl Sep 17 '15 at 11:57