ruby way to enumerate users

R

Robert K.

Hi,
exists there a preferred ruby way to enumerate the
users names and directories?


especially on linux
 
R

Robert Klemme

Robert K. said:
Hi,
exists there a preferred ruby way to enumerate the
users names and directories?


especially on linux

Maybe something like this

ruby -naF: -e 'printf "%s\t%s\n", $F[0], $F[5]' /etc/passwd

robert
 
R

Robert Klemme

Robert K. said:
Hi,
exists there a preferred ruby way to enumerate the
users names and directories?


especially on linux

Maybe something like this

ruby -naF: -e 'printf "%s\t%s\n", $F[0], $F[5]' /etc/passwd

robert
 
R

Robert K.

Maybe something like this

ruby -naF: -e 'printf "%s\t%s\n", $F[0], $F[5]' /etc/passwd

Is this allways applicable? For example in an NIS+ env
- that's why I'm asking.



THX
 
R

Robert K.

I don't see any such thing in the standard library, RAA, or Ruby Forge;
maybe I'm just not looking in the right place, or maybe you have just
discovered your first module project! :)

Others: Is this the case?
Do I have to or may I develope that module?
 
G

Gavin Sinclair

Maybe something like this

ruby -naF: -e 'printf "%s\t%s\n", $F[0], $F[5]' /etc/passwd

Is this always applicable? For example in an NIS+ env
- that's why I'm asking.
Definitely not. NIS, NIS+, NetInfo, non-UNIX POSIX environments, . . .
all sorts of different places the info could be. What is needed is a
Ruby interface to the getpwent() family of system calls.
I don't see any such thing in the standard library, RAA, or Ruby Forge;
maybe I'm just not looking in the right place, or maybe you have just
discovered your first module project! :)

require 'etc'

(Should be in PickAxe, definitely in Nutshell.)

Gavin
 
E

Eric Sunshine

Okay, so enumerating users is easy enough.
require 'etc'
class Users
include Etc
include Enumerable
def each
setpwent
while pw = getpwent
yield pw
end
endpwent
end
end
Users.new.each { |u| puts u.name }

Simpler:

require 'etc'
class Users
include Etc
include Enumerable
alias :each :passwd
public :each
end

Users.new.each { |u| puts u.name }
 
D

Daniel Berger

Eric Sunshine said:
Simpler:

require 'etc'
class Users
include Etc
include Enumerable
alias :each :passwd
public :each
end

Users.new.each { |u| puts u.name }

Apparently my earlier message got lost. There is no need to wrap this
with the etc module.

require "etc"
Etc.passwd{ |s|
p s.name
p s.dir
}

This is documented in the Nutshell book and at
http://www.rubygarden.org/ruby?ProgrammingRubyTwo/Etc

Regards,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top