From: Ryan Pavlik Date: 2003-10-03T10:38:11+09:00 Subject: Re: backquotes, system, shell and ruby On Fri, 3 Oct 2003 10:16:53 +0900 John Carter wrote: > Somebody suggested I use zsh instead of bash for my commandline, so I > looked at it, looked at the syntax for tweaking the profile files, and > said... > "Yuck! Another badly designed syntax! Why can't I just use Ruby?" It's sh. Deal with it. ;-) It's not great, but it's not so bad, either. I would, however, not mind having a ruby shell. But I also don't expect to use ruby---which is designed to be suitable primarily as a programming language---to *directly* fit the bill. > And that got me thinking. > > Consider backquotes. > Why? Why does a scripting language like Ruby keep invoking an old and bad > scripting language call shell? I'm not sure what you're getting at here. Yes, it ran a shell, because that's what system() does. You can even do it in C, although C doesn't use backticks as a shorthand. > Because... > > a) Ruby doesn't have a good syntax for invoking and dealing with > processes and pipes between processes. You mean like File.popen? > b) Ruby doesn't have a built in understanding of ENV['PATH'] The OS understands the path for you. Ruby doesn't need to. > c) Ruby doesn't have such a simple syntax for globbing (beyond > Dir['*.c']) Ruby isn't a shell, either, and although it is a scripting language (which explains the presence of ``), this doesn't automatically make it suitable for a drop-in shell replacement. > Consider the "rename" facility that comes with perl these days. > Ooh! Looky! Nice! Wouldn't it be nice to have the power of ruby > intertwingled with the commandline. At times I fire up irb to take care of similar complex tasks. > Ruby does have Shell.rb, which takes us halfway there. (If it had > decent docs...) > > How hard would it be to extend the backtick syntax to be a pure ruby > thing. ie. So proc_exec doesn't invoke bash, but invokes Shell.rb? > > > Hmm. Could we even do this entirely in UserLand? ie. Couldn't we > override proc_exec and replace it with ruby code that parses the > string, handles the redirects, variable interpolation quoting and > forks, then just directly execs the program without ever touching sh? What you need to do is simply write a shell that uses ruby instead of its own sh-like syntax. Modifying ruby is not the answer. Using ruby and a little readline loop with some decent parsing (calls to fork, popen, etc.: process and IPC handling) is. I must say I have wanted something like this for awhile. I even came up with a really nice syntax for integrating shell commands and ruby, although I forget off the top of my head what it was... it'd be _fairly_ easy to do. Just don't try to modify ruby to do it, because it's not the right solution. -- Ryan Pavlik "Well what was it, Mr. Nightmare Pants?" - 8BT