From: Josh Cheek Date: 2010-07-02T09:02:13+09:00 Subject: Re: Running a shell command from ruby --0015173ff3ac5de80a048a5c4cd6 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Jul 1, 2010 at 2:53 PM, Steve Pauly wrote: > I am experimenting with running bash commands from within ruby. The > commands should be in an array, separated internally with semicolons. > > The problem is that they do not get executed. > > My script: > > ---- > #!/usr/bin/ruby > script1=["touch ~/ruby/f1;touch ~/ruby/f2"] #addl array elements to > be added > puts script1[0].split(/;/) do |k| > puts k > system(k) > end > ---- > > The output from "puts k" looks good. > But why do the files f1 and f2 not get created? > > I realize that executing commands this way in ruby may be bad practise, > but at least, why do the commands not execute? > > Thanks in advance. > Steve. > -- > Posted via http://www.ruby-forum.com/. > > It has to do with block bindings. do ... end bind to the furthest left method call on the line, so in this case, the block is being passed to the puts method, not the split method. def puts( *args , &block ) super "puts received: #{args.inspect}, and #{block ? 'a' : 'no'} block." end script1=["touch ~/ruby/f1;touch ~/ruby/f2"] #addl array elements to be added puts script1[0].split(/;/) do |k| puts k system(k) end --0015173ff3ac5de80a048a5c4cd6--