I'm relatively new to ruby and to practice I've put together the code
below - it's supposed take two arguments then apply it to a grep command
and display the output. Currently, nothing is outputted to the console.
Anyone see what I'm doing wrong, thanks.
I'm relatively new to ruby and to practice I've put together the code
below - it's supposed take two arguments then apply it to a grep command
and display the output. Currently, nothing is outputted to the console.
Anyone see what I'm doing wrong, thanks.
A few remarks: the block form of IO.popen is more robust because it ensures proper closing of file handles. Also, using an array as first argument avoids quoting issues - you need 1.9 for this. Thus you could do:
IO.popen ["grep", "-i", *ARGV[0..1]], "r" do |io|
io.each_line do |line|
puts line
end
end
However, much easier would be to read and grep yourself in Ruby:
rx = Regexp.new(ARGV[0], Regexp::IGNORECASE)
File.foreach ARGV[1] do |line|
puts line if rx =~ line
end
Kind regards
robert
···
On 04/12/2010 01:40 AM, Dan King wrote:
I'm relatively new to ruby and to practice I've put together the code
below - it's supposed take two arguments then apply it to a grep command
and display the output. Currently, nothing is outputted to the console.
Anyone see what I'm doing wrong, thanks.