Silly(?) request


#1

Does anyone know if it’s possible to pipe the output of a command to a
running instance of irb?

I said it was silly.
But it seems like it would be really useful. Something along the lines
of
#nix>find ./ -name ".foo" |toirb
that would dump the output from find (as an array? As text?) to an
already active instance of IRB.

Does such a thing exist?

If not I feel confident enough to try and butcher up something like it.

My guess is the instance of IRB would have to instantiate some object
(lets call it P), and then when the |toruby command was used, the P
object could be queried for the list.

Any thoughts?

–Kyle


#2

Kyle S. wrote:

Does anyone know if it’s possible to pipe the output of a command to a
running instance of irb?

I said it was silly.
But it seems like it would be really useful. Something along the lines of
#nix>find ./ -name ".foo" |toirb
that would dump the output from find (as an array? As text?) to an
already active instance of IRB.

Try

mkfifo file
find ./ -name “*.foo” > file

irb> File.open(“file”, “r”)

That should do the trick.

Vince


#3

Nahh, there must be some slick way of doing this :slight_smile:

–Kyle


#4

I’ve always been a fan of the

p = %x(find ./ -name “*.c”).split("\n")

— Kyle S. removed_email_address@domain.invalid wrote:

Nahh, there must be some slick way of doing this :slight_smile:

–Kyle

I don’t mean to say that I have already achieved these things or that I
have already reached perfection! But I keep working toward that day
when I will finally be all that Christ Jesus saved me for and wants me
to be. Philip. 3: 12, NLT
http://www.iit.edu/~meyedou/

  • Doug Meyer

#5

Kyle S. wrote:

Nahh, there must be some slick way of doing this :slight_smile:

Sorry, I didn’t realize you were probably not handling a huge amount
of data… in which case, it doesn’t really matter if the file is a fifo
or not.

You still can do the same trick with sockets, though, but it won’t
come much easier… The main problem is you need a server somewhere.
Named pipes are an option for this, as well as tcp networking (though
the latter is admittedly way more heavy). Apart from that, I don’t see
other techniques, unless you want to dwell into semaphores (but I’m not
an expert in IPC).

Cheers,

Vince


#6

Well, partially because the find command was an example. I want to be
able to pipe the output of ANY program on my machine into a running
irb.

Yea I’m silly.

But I’ll spend some time reading up on IPC and how similar things work…


#7

On 05.03.2007 23:26, doug meyer wrote:

I’ve always been a fan of the

p = %x(find ./ -name “*.c”).split("\n")

Why not

result = Dir["**/*.c"]

:slight_smile:

Kind regards

robert


#8

On 05.03.2007 23:20, Kyle S. wrote:

Nahh, there must be some slick way of doing this :slight_smile:

You could set up a DRb server in IRB and make “toruby” an DRb client
that sends data off to the server. In any case you have solve the
problem how toruby knows which IRB to talk to. Either you allow just
one IRB session on your system or you have to provide toruby with some
kind of input that shows him the way (env var, command line arg).

Personally I don’t feel that this is very useful. There is `` and %x{}
as was shown already. And for finding files there is Dir[] and
Find.find which both work without any external process.

Kind regards

robert


#9

I think I chose a bad example, I don’t want to just find things :slight_smile: but
it looks like that’s the problem people have aimed at.

The %x{} is very useful for some things I admit.

I’ve got some areas where a pipe-to-ruby would be really nice. I’m
sure if it’s there I’ll find other uses for it too.

Good point about the choice of which irb to send it to though. I was
planning on looking into how screen finds screens to attach to. If
only one is open that belongs to that user, it automagically attaches
to that one, if there is more than one, it politely coughs and
requests that you specify one. Sadly that does make things a little
more complex.

Lets just settle on, I’m going to write this since it doesn’t exist in
the form I want to use. And I’m going to write it as an object the
client has to open.

Now, moving on… Would something like this seem bad (style in ruby,
in general, or in functionality)
in irb-----------
require ‘silly/Kpipe’
mine=Kpipe.new
puts mine.id

1701

then in the shell (a non find example!)-----------
#*nix> mpg123 silly.mp3 -s |toruby 1701

then back in irb-----
mine.each_byte{|i| likeiknowwhattodowithwavdata(i) }


#10

On Tue, Mar 06, 2007 at 08:09:48AM +0900, Kyle S. wrote:

I think I chose a bad example, I don’t want to just find things :slight_smile: but
it looks like that’s the problem people have aimed at.

The %x{} is very useful for some things I admit.

It seems to me that what you want is not to send the command’s output
directly into irb (which would treat it as a series of ruby commands and
try
to execute them), but to stuff the command’s output into a pipe, which
you
can then read from using Ruby in irb at your leisure.

This is easy if you issue the shell command itself within irb:

irb(main):001:0> IO.popen(“cat /etc/passwd”,“r”).each_line { |x| puts x
}
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh

Otherwise, you can use a fifo (named pipe).

[screen 1]
$ mkfifo toirb
$ cat /etc/passwd >toirb

[screen 2]
$ irb
irb(main):001:0> File.open(“toirb”) { |f| f.each_line { |x| puts x } }
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh

=> #<File:toirb (closed)>
irb(main):002:0>

This easily solves the problem of which irb process the output gets
directed
to - both the sending side and the receiving side refer to the pipe by
name.
You can of source make your own syntactic sugar wrappers.

HTH,

Brian.


#11

On 06.03.2007 00:09, Kyle S. wrote:

only one is open that belongs to that user, it automagically attaches
in irb-----------
require ‘silly/Kpipe’
mine=Kpipe.new
puts mine.id

1701

then in the shell (a non find example!)-----------
#*nix> mpg123 silly.mp3 -s |toruby 1701

then back in irb-----
mine.each_byte{|i| likeiknowwhattodowithwavdata(i) }

Yeah, you could use the port number (probably using Unix domain sockets)
as that id.

Btw, how do you want to deal with multiple submissions? Do you want to
treat them in IRB as a single stream or as individual streams? The
first is probably easier with a pipe or a file, the latter seems easier
with a socket.

Have fun!

robert


#12

I’m thinking multiple streams maybe useful. Guess I know what I’m
doing this weekend :slight_smile: heck, maybe even implement naive versions of
both to see.

–Kyle