Forum: Ruby silly(?) request

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kyle S. (Guest)
on 2007-03-06 00:13
(Received via mailing list)
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
Vince H&K (Guest)
on 2007-03-06 00:19
(Received via mailing list)
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
Kyle S. (Guest)
on 2007-03-06 00:21
(Received via mailing list)
Nahh, there must be some slick way of doing this :)

--Kyle
doug meyer (Guest)
on 2007-03-06 00:27
(Received via mailing list)
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 :)
>
> --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
Vince H&K (Guest)
on 2007-03-06 00:31
(Received via mailing list)
Kyle S. wrote:
> Nahh, there must be some slick way of doing this :)

  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
Robert K. (Guest)
on 2007-03-06 00:41
(Received via mailing list)
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"]

:-)

Kind regards

  robert
Kyle S. (Guest)
on 2007-03-06 00:45
(Received via mailing list)
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..
Robert K. (Guest)
on 2007-03-06 00:45
(Received via mailing list)
On 05.03.2007 23:20, Kyle S. wrote:
> Nahh, there must be some slick way of doing this :)

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
Kyle S. (Guest)
on 2007-03-06 01:10
(Received via mailing list)
I think I chose a bad example, I don't want to just find things :) 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) }
Robert K. (Guest)
on 2007-03-06 10:15
(Received via mailing list)
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
Brian C. (Guest)
on 2007-03-06 10:56
(Received via mailing list)
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 :) 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.
Kyle S. (Guest)
on 2007-03-06 16:29
(Received via mailing list)
I'm thinking multiple streams maybe useful.  Guess I know what I'm
doing this weekend :) heck, maybe even implement naive versions of
both to see.

--Kyle
This topic is locked and can not be replied to.