On Thu, Jul 1, 2010 at 7:02 PM, Josh C. [email protected] wrote:
#!/usr/bin/ruby
It has to do with block bindings. do … end bind to the furthest left
script1=[“touch ~/ruby/f1;touch ~/ruby/f2”] #addl array elements to be
added
puts script1[0].split(/;/) do |k|
puts k
system(k)
end
Here is a solution that creates the files.
script1=[“touch ~/f1;touch ~/f2”] #addl array elements to be added
script1[0].split(/;/).each do |k|
puts k
system k
end
Note that {} has the kind of binding that you were looking for, but that
split does not take a block (which is why I added the each method).
though I don’t understand your format, are you wanting them to be
separated
by being elements in an array, or by splitting a string on semicolons?
Right
now, any other elements in this array will be ignored, because you only
look
at the one in index zero. (what is the point of the Array?)
But you also have an issue if you split on semicolons, that assumes all
semicolons are valid delimiters. Depending on context, this may not be
the
case. For example, what if the String was:
%{echo “if ARGV.empty? ; puts ‘no args’ else puts ‘got args’ end” >
argchecker.rb ; ruby argchecker.rb 1 2 3 ; ruby argchecker.rb}
Then you want it to find these commands
[
"echo "if ARGV.empty? ; puts ‘no args’ else puts ‘got args’ end" >
argchecker.rb ",
" ruby argchecker.rb 1 2 3 “,
" ruby argchecker.rb”
]
But split(‘;’) will give you these commands
[
"echo "if ARGV.empty? ",
" puts ‘no args’ else puts ‘got args’ end" > argchecker.rb ",
" ruby argchecker.rb 1 2 3 “,
" ruby argchecker.rb”
]
I think the best solution (easiest to implement, least likely to have
bugs,
most straightforward) is to keep each command separated in the Array,
and
not try to pull them from the string. If you do need to do that, you
must
either be careful to avoid using a semicolon in a way that is not a
command
delimiter, or you must find/write a parser so that you can determine
where
commands begin and end. I suppose you could also pick a different
delimiter
that is really unlikely to ever show up outside of the delimiter
context.
Some obscure unicode character, maybe.