# Free disk space with VMI in megabyte

**URL:** <https://www.ruby-forum.com/t/free-disk-space-with-vmi-in-megabyte/135946>\
**Category:** Ruby\
**Created:** [April 26, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/free-disk-space-with-vmi-in-megabyte/135946 "2008-04-26T19:48:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 26, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/free-disk-space-with-vmi-in-megabyte/135946/1 "2008-04-26T19:48:02Z")

</div>

Hi all!

I’m new to ruby, I’ve found this pretty neat code linked on a thread on  
this forum, original post was at:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/95197](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/95197)

require ‘win32ole’

wmi = WIN32OLE.connect(“winmgmts://./root/cimv2”)  
disk = wmi.ExecQuery(“Select \* from Win32\_LogicalDisk”)  
disk.each {|drive| puts “#{drive.DeviceID} #{drive.FreeSpace}”}

Now, the code work very well, but I can’t convert the free space in MB  
or any other unit, how can I convert it?

Thanks.

Best regards.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 27, 2008, 4:40am UTC](https://www.ruby-forum.com/t/free-disk-space-with-vmi-in-megabyte/135946/2 "2008-04-27T04:40:55Z")

</div>

Toki T. wrote:

> disk.each {|drive| puts “#{drive.DeviceID} #{drive.FreeSpace}”}
> 
> Now, the code work very well, but I can’t convert the free space in MB  
> or any other unit, how can I convert it?
> 
> Thanks.
> 
> Best regards.

Hello Toki:

The drive.FreeSpace is being returned as a string (which may also be a  
nil). Change #{drive.FreeSpace} to #{drive.FreeSpace.class} to see what  
I mean. You’ll need to convert the FreeSpace to a number before you can  
use math operators / functions with it. Below is an example of how to  
do that with the #to\_f (aka “to float”) method:

disk.each {|drive| puts “#{drive.DeviceID} #{(drive.FreeSpace.to\_f /  
1024) / 1024}”}

Hope that helps,

Michael

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 27, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/free-disk-space-with-vmi-in-megabyte/135946/3 "2008-04-27T22:54:18Z")

</div>

Michael B. wrote:

> Hello Toki:
> 
> The drive.FreeSpace is being returned as a string (which may also be a  
> nil). Change #{drive.FreeSpace} to #{drive.FreeSpace.class} to see what  
> I mean. You’ll need to convert the FreeSpace to a number before you can  
> use math operators / functions with it. Below is an example of how to  
> do that with the #to\_f (aka “to float”) method:
> 
> disk.each {|drive| puts “#{drive.DeviceID} #{(drive.FreeSpace.to\_f /  
> 1024) / 1024}”}
> 
> Hope that helps,
> 
> Michael

Thanks a lot for the help and the explanation, I didn’t know that  
“FreeSpace” by default is returned as a string.

Best regards.
