Shell commands in irb

Hi

I was quite bored this morning and the coffee machine went into its 30
minute clean cycle, so I came up with this…

$ cat .irbrc
Dir["{,/usr,/usr/local}/bin/*"].each do |bin|
Object.instance_eval do
define_method(File.basename(bin)) do |*args|
args.map! { |arg| arg.to_s }
system bin, *args
end
end
end

$ irb --simple-prompt
irb: warn: can’t alias kill from irb_kill.

touch :foo
=> true

mv :foo, :bar
=> true

ls “-l”, :bar
-rw-r–r-- 1 andre andre 0 Oct 16 11:56 bar
=> true

Probably useless, I know… but running “vim :foo.rb” or “top” from
inside irb is cool :stuck_out_tongue:

Andre

On Mon, 16 Oct 2006, Andre N. wrote:

 system bin, *args

end
end
end

cool idea. i tweaked it to do lazy method definition:

 harp:~ > cat .irbrc
 module Kernel
   PATH = ENV['PATH'].split File::PATH_SEPARATOR
 end

 class Object
   def self.binattr bin
     bin = bin.to_s
     m = File.basename bin
     unless respond_to?(m)
       define_method(m){|*a| system bin, *a.map{|s| s.to_s}}
     end
   end

   def which bin
     b = bin.to_s
     if b =~ %r|^#{ File::SEPARATOR }|
       b
     else
       dir = PATH.detect{|d| test(?e, File.join(d,b))}
       dir ? File.join(dir,b) : nil
     end
   end

   alias_method "__mm__", "method_missing"

   def method_missing m, *a, &b
     bin = which m
     if bin
       Object.binattr(bin) and send(m,*a,&b)
     else
       __mm__(m,*a,&b)
     end
   end
 end

fun stuff!

-a

On Tue, 2006-10-17 at 00:01 +0900, [email protected] wrote:

cool idea. i tweaked it to do lazy method definition:

Heh, sweet. If only we didn’t need the commas and the quotes around
command flags…

Andre

Andre N. wrote:

On Tue, 2006-10-17 at 00:01 +0900, [email protected] wrote:

cool idea. i tweaked it to do lazy method definition:

Heh, sweet. If only we didn’t need the commas and the quotes around
command flags…

Yes, but it’s better than having to use $var to evaluate a variable :wink:

On Tue, 2006-10-17 at 02:22 +0900, Joel VanderWerf wrote:

Yes, but it’s better than having to use $var to evaluate a variable :wink:

Ew, no kidding! :slight_smile:

Andre

On 2006.10.16 23:10, Andre N. wrote:

  system bin, *args

ls “-l”, :bar
-rw-r–r-- 1 andre andre 0 Oct 16 11:56 bar
=> true

Probably useless, I know… but running “vim :foo.rb” or “top” from
inside irb is cool :stuck_out_tongue:

You might find rs (the Ruby shell project) of interest! One of the
core tenets is to provide full access to Ruby at the command-line.

http://projects.kittensoft.org/rs/rdoc/indox.html

Hi Eero,

On 10/16/06, Eero S. [email protected] wrote:

You might find rs (the Ruby shell project) of interest! One of the
core tenets is to provide full access to Ruby at the command-line.

http://projects.kittensoft.org/rs/rdoc/indox.html

Your project looks very interesting, but the above URL gave me a “Page
Not Found” error. However,

http://projects.kittensoft.org/rs/rdoc/index.html

works fine.

Wayne


Wayne V.
No Bugs Software
Ruby, C#, and Erlang Agile Contract Programming in Silicon Valley