How to get special directories?

hi all,

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/.

Niklas Baumstark wrote:

hi all,

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

Greetings,
Niklas

I'm not sure if this'll work, but "/" should get you the home folder in
linux, and "C:/Documents and Settings" should work fine on Windows.

···

--
Posted via http://www.ruby-forum.com/\.

Have you looked at Ruby's ENV environment hash? Try:

  p ENV

to see what's available on your OS.

···

--
Posted via http://www.ruby-forum.com/.

To get folders like 'documents and settings' on windows you need to use win32api. Depending on language, these may have different names.

···

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

"/" should get you the home folder in linux

surely it should not.

and "C:/Documents and Settings" should work fine on Windows.

well, my home dir is under D:/home...

but you're right, i could use File.expand_path("~") under linux. i could
maybe use COM under windows and use the scripting host to get the
directory. but this isn't very elegant. is there no better way? if there
is none: is it possible to access COM from ruby?

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.

Dave Bass wrote:

Have you looked at Ruby's ENV environment hash? Try:

  p ENV

to see what's available on your OS.

sry, haven't seen this before posting. the problem is, that it's an app
that is shared between linux, windows and maybe OS X machines, so it
must be cross-platform. and ENV is not globally available. i've had the
sitation recently, that a SVN post-commit hook-script didn't inherit its
variables from the environment so the ruby script it called didn't have
any environment variables to use.

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.

Dave Bass wrote:

Have you looked at Ruby's ENV environment hash?

Not that it makes any kind of a difference, but ENV isn't actually a hash.
Just thought I'd mention that for correctness' sake.

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Eric Hodel wrote:

···

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.

okay so i have to make a platform switch... is it possible that you
could give an example of what API-Call to do and how?

Greetings,
Niklas
--
Posted via http://www.ruby-forum.com/\.

Eric Hodel wrote:

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.

You can also use the win32-dir gem

C:\>gem in win32-dir
Successfully installed win32-dir-0.3.2
1 gem installed
C:\>irb

require 'win32/dir'

=> true

Dir::PROFILE

=> "C:\\Documents and Settings\\gthiesfeld"

···

On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark <niklas.baumstark@gmail.com> wrote:

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:

Hi,

At Fri, 1 Aug 2008 01:36:20 +0900,
Niklas Baumstark wrote in [ruby-talk:309687]:

but you're right, i could use File.expand_path("~") under linux. i could
maybe use COM under windows and use the scripting host to get the
directory. but this isn't very elegant. is there no better way? if there
is none: is it possible to access COM from ruby?

It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
and is the cross-platform and official recommended way.

···

--
Nobu Nakada

Gordon Thiesfeld wrote:

···

On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark > <niklas.baumstark@gmail.com> wrote:

Eric Hodel wrote:

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.

You can also use the win32-dir gem

C:\>gem in win32-dir
Successfully installed win32-dir-0.3.2
1 gem installed
C:\>irb

require 'win32/dir'

=> true

Dir::PROFILE

=> "C:\\Documents and Settings\\gthiesfeld"

Thanks that'll help alot! Is something similiar available on linux as
well?

Greetings,
Niklas
--
Posted via http://www.ruby-forum.com/\.

Nobuyoshi Nakada wrote:

It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
and is the cross-platform and official recommended way.

File.expand_path("~") ?

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.

Dunno about linux, but here's something that will work on mac os x.
The way mac os x tracks things like disk is a combination of a domain and a folder type.
Domains are thinks like user domain, local domain etc..., folder types are things like 'applications folder', 'library folder' and so on.
So (in C) the user's library folder (/Users/fred/Library in my case) is identified by kUserDomain, kDomainLibraryFolderType and the top level one (/Library) is identified by
kLocalDomain, kDomainLibraryFolderType

The following code uses rubyinline to call some C code. This doesn't seem to work if you paste it into irb, but as a standalone ruby file runs fine

require 'rubygems'
gem 'RubyInline'
require 'inline'

class Folder
   module Domains
     USER = -32763
     SYSTEM_DISK = -32768
     SYSTEM = -32766
     LOCAL = -32765
   end

   module Types
     def self.int_for_code code
       code.unpack('N').first
     end

     TOP = int_for_code 'dtop' #more of these constants in Folders.h
     DOCUMENTS = int_for_code 'docs'
   end

   inline do |builder|
     builder.include "<CoreServices/CoreServices.h>"
     builder.add_compile_flags "-framework CoreServices"
     builder.c_singleton "
       static char *find(int domain, unsigned long folder_type){
         char c_path[PATH_MAX];
         FSRef folder;
         if(noErr == FSFindFolder(domain, folder_type, true, &folder)){
           if(noErr ==FSRefMakePath(&folder, (UInt8*)c_path, PATH_MAX))
             return c_path;
         }
         /* decide what you want to do in case of failure */
         return \"err\";
       }
     "
   end
end

puts Folder::find(Folder::Domains::USER, Folder::Types::DOCUMENTS) #should output the user's documents folder
puts Folder::find(Folder::Domains::USER, Folder::Types::TOP) #should output the top level of the user domain, ie the home folder

···

On 31 Jul 2008, at 18:29, Niklas Baumstark wrote:

Gordon Thiesfeld wrote:

On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark >> <niklas.baumstark@gmail.com> wrote:

Eric Hodel wrote:

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.

You can also use the win32-dir gem

C:\>gem in win32-dir
Successfully installed win32-dir-0.3.2
1 gem installed
C:\>irb

require 'win32/dir'

=> true

Dir::PROFILE

=> "C:\\Documents and Settings\\gthiesfeld"

Thanks that'll help alot! Is something similiar available on linux as
well?

Niklas Baumstark wrote:

Gordon Thiesfeld wrote:

require 'win32/dir'

=> true

Dir::PROFILE

=> "C:\\Documents and Settings\\gthiesfeld"

Thanks that'll help alot! Is something similiar available on linux as
well?

Greetings,
Niklas

Can't test, but on linux

require 'etc'
Etc.getpwuid(0) #needs integer user id

should work. (
http://www.ruby-doc.org/stdlib/libdoc/etc/rdoc/classes/Etc.html#M000661
)

hth,

Siep

···

--
Posted via http://www.ruby-forum.com/\.

Hi,

At Fri, 1 Aug 2008 16:29:08 +0900,
Niklas Baumstark wrote in [ruby-talk:309767]:

> It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
> and is the cross-platform and official recommended way.

File.expand_path("~") ?

Yes.

···

--
Nobu Nakada

Siep Korteling wrote:

Niklas Baumstark wrote:

Gordon Thiesfeld wrote:

require 'win32/dir'

=> true

Dir::PROFILE

=> "C:\\Documents and Settings\\gthiesfeld"

Thanks that'll help alot! Is something similiar available on linux as
well?

Greetings,
Niklas

Can't test, but on linux

require 'etc'
Etc.getpwuid(0) #needs integer user id

should work. (
http://www.ruby-doc.org/stdlib/libdoc/etc/rdoc/classes/Etc.html#M000661
)

hth,

Siep

thank you all for your answers, i'm now gonna use win32-dir and etc,
ignoring OS X. can't test it there.

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.

Nobuyoshi Nakada wrote:

Hi,

At Fri, 1 Aug 2008 16:29:08 +0900,
Niklas Baumstark wrote in [ruby-talk:309767]:

> It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
> and is the cross-platform and official recommended way.

File.expand_path("~") ?

Yes.

irb(main):001:0> File.expand_path('~')
ArgumentError: couldn't find HOME environment -- expanding `~'
        from (irb):1:in `expand_path'
        from (irb):1

under Windows XP Pro.

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.

Niklas Baumstark wrote:

irb(main):001:0> File.expand_path('~')
ArgumentError: couldn't find HOME environment -- expanding `~'
        from (irb):1:in `expand_path'
        from (irb):1

under Windows XP Pro.

Greetings,
Niklas

oh sorry! got an old version of ruby:

C:\>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Greetings,
Niklas

···

--
Posted via http://www.ruby-forum.com/\.