New to ruby,how to make FIFO in ruby?

Hi,

Is there anyone can tell me how to make FIFO in ruby? I cannot find the
related method? Thanks.

Maggie

Well, you use a Array push and shift. If you want this behavior
could be coded as:

class Simple_FIFO
def initialize
@data = Array.new
end

def push(val)
@data.push(val)
end

def pop(val)
@data.shift(val)
end
end

Error handling is left as an excercise to the reader :slight_smile:

  • alan
···

On Fri, Jul 12, 2002 at 05:17:20AM +0900, Maggie Xiao wrote:

Hi,

Is there anyone can tell me how to make FIFO in ruby? I cannot find the
related method? Thanks.

Maggie


Alan Chen
Digikata LLC

Maggie Xiao graced us by uttering:

Is there anyone can tell me how to make FIFO in ruby? I cannot find
the related method? Thanks.

When I ported a Perl script that used (Unix special filetype) FIFOs, I
simply used Kernal::system to call the external executable.

Another option might be to use the mkfifo(3) POSIX call on systems that
support it.

If anyone knows of a more graceful way to accomplish this, I’d be very
interested in hearing it.

Just as a (somewhat anal) optimization/efficiency issue, backticks would
be overkill, as mknod(1)/mkfifo(1) don’t write anything to stdout. It’s
sufficient to run under Kernel::system and check the return value,
rather than run it in backticks and capture output that may not exists
into a variable that may not even be present.

A similar, Perl-related issue can be found here (if you have the perldoc
command):

$ perldoc -q map

or here:

http://www.perldoc.com/perl5.6.1/pod/perlfaq6.html

--> "What's wrong with using grep or map in a void context?"

Maggie

HTH
Tim Hammerquist

···


Westley: Why didn’t you wait for me?
Buttercup: Well, you were dead.
– “The Princess Bride”

I did something like this a while back:

class File
def self.mkfifo(name, mode=“0600”, open_mode=“rw”)
if ! File.writeable? File.dirname(name)
raise Errno::EACCES, "No such directory"
end

if File.exists? name and File.pipe? name # Leftover from before
  File.delete name
end

if ! File.exists? name
  system("mkfifo", "--mode=" + mode, name)
  @@created_fifos << name
end

return File.open(name, open_mode)
···

On Thu, 2002-07-11 at 15:17, Maggie Xiao wrote:

Hi,

Is there anyone can tell me how to make FIFO in ruby? I cannot find the
related method? Thanks.

[snip]

I think perhaps she means a UNIX special
file, as in mkfifo… If so, I’m not aware
of any particular support for this, though.

Hal Fulton

···

----- Original Message -----
From: “Alan Chen” alan@digikata.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 11, 2002 3:46 PM
Subject: Re: new to ruby,how to make FIFO in ruby?

On Fri, Jul 12, 2002 at 05:17:20AM +0900, Maggie Xiao wrote:

Hi,

Is there anyone can tell me how to make FIFO in ruby? I cannot find the
related method? Thanks.

Maggie

Well, you use a Array push and shift. If you want this behavior
could be coded as:

Augh! I accidentally bumped send before I was done:

class File
def self.mkfifo(name, mode=“0600”, open_mode=“rw”)
if ! File.writeable? File.dirname(name)
raise Errno::EACCES, "No such directory"
end

if File.exists? name and File.pipe? name # Leftover from before
  File.delete name
end

if ! File.exists? name
  system("mkfifo", "--mode=" + mode, name)
  @@created_fifos << name
end

return File.open(name, open_mode)

end

def self.delete_fifos
@@created_fifos.each { |fifo| File.delete fifo }
end
end

This code doesn’t work perfectly (if the file exists and is not a
FIFO, it will just open the file normally, which was intended behavior
for my program, but probably not for yours). And the File.delete_fifos
code is probably not mandatory, but usually programs want to clean up
their FIFOs when they exist. Also, you might not want it to return the
file, in case you want to do your own stuff to make it non-blocking,
etc.

···

Hmm… Sorry about that. I’ve done too much embedded programming in the past :slight_smile:

So wouldn’t one just use the command line mkfifo and open it
as a regular file?

···

On Fri, Jul 12, 2002 at 06:45:43AM +0900, Hal E. Fulton wrote:

----- Original Message -----
From: “Alan Chen” alan@digikata.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 11, 2002 3:46 PM
Subject: Re: new to ruby,how to make FIFO in ruby?

On Fri, Jul 12, 2002 at 05:17:20AM +0900, Maggie Xiao wrote:

Hi,

Is there anyone can tell me how to make FIFO in ruby? I cannot find the
related method? Thanks.

Maggie

Well, you use a Array push and shift. If you want this behavior
could be coded as:

[snip]

I think perhaps she means a UNIX special
file, as in mkfifo… If so, I’m not aware
of any particular support for this, though.

Hal Fulton


Alan Chen
Digikata LLC

Hmm… Sorry about that. I’ve done too much embedded programming in the
past :slight_smile:

Don’t apologize, as I’m not sure I’m
interpreting this correctly.

Maggie? Can you clarify?

So wouldn’t one just use the command line mkfifo and open it
as a regular file?

I would think so… if it had to be done
in Ruby, there always backtick notation…
I haven’t done that kind of thing enough
to know whether there’s any notable
advantage to avoiding the command line
utility.

Hal

···

----- Original Message -----
From: “Alan Chen” alan@digikata.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 11, 2002 4:56 PM
Subject: Re: new to ruby,how to make FIFO in ruby?