Hi,
I’m new to Ruby, and so I was trying some stuff I was used to do with
ease in perl. Something like capturing and storing the output of a
commandline app in an array and manipulate that array afterwards.
After searching the net, I finally found the following solution (capture
output, then slice unecessary stuff away). Unfortunately slicing doesn’t
work when i’m using open3 to capture the output. Question: what am I
doing wrong?
I attached the working code + the method I want to modify:
# new method, that I want to use (but I also need to delete
# the first 5 lines of the dpkg output
def collect_inst_packages
require 'open3'
stdin, @InstalledPackageList, stderr =
Open3.popen3("/usr/bin/dpkg -l")
# cut away the first 5 lines
end
What is the error message you recieve, and what is the content of the
array? It should work (your .slice cuts away the first some members of
the array) but its a bit hard to see whats going on (i dont have dpkg
here to test)
Btw “class dpkg” is unusual in that the first char is lowercased.
Typical ruby code will normally be like so “class Dpkg” (just look at
Array.new which is “class Array” or String.new which is “class String”)
By the way I also recommend you to do this during debugging:
require ‘pp’
And at your stdin, stderr line try a
pp stdin
pp stderr
pp @InstalledPackageList
yes is a primitive way to “debug” but very quick and pp makes it look
pretty
(and if some code block fails, you can always but begin;rescue;end
around it or even catch the error with a
begin
your critical code here
rescue Exception => e
pp "error was "
pp e
end
@installedPackageList is an IO object, not an array. You need to read
its contents, convert it into an array, and then remove things
(readlines does steps one and two at once): @InstalledPackageList = @InstalledPackageList.readlines[5…-1]
daniel@daniel-desktop:~/lightconsolegem$ ri IO#readlines
----------------------------------------------------------- IO#readlines
ios.readlines(sep_string=$/) => array
Reads all of the lines in ios, and returns them in anArray. Lines
are separated by the optional sep_string. If sep_string is nil,
the rest of the stream is returned as a single record. The stream
must be opened for reading or an IOError will be raised.
f = File.new("testfile")
f.readlines[0] #=> "This is line one\n"
What is the error message you recieve, and what is the content of the
array? It should work (your .slice cuts away the first some members of
the array) but its a bit hard to see whats going on (i dont have dpkg
here to test)
Btw “class dpkg” is unusual in that the first char is lowercased.
Typical ruby code will normally be like so “class Dpkg” (just look at
Array.new which is “class Array” or String.new which is “class String”)
Actually, class dpkg won’t run:
$ ruby -e “class dpkg; end”
-e:1: class/module name must be CONSTANT
The class keyword will work with either a constant, or a “<< object”
expression (for singleton classes).
On Mon, Apr 14, 2008 at 1:41 AM, David A. Black [email protected]
wrote:
expression (for singleton classes).
dpkg = Class::new{…}
will work but is a little bit against convention.
However very useful in testing (allowing you to define your classes in
the test methods
see e.g. my alltime favorite RubyQuiz Ruby Quiz - metakoans.rb (#67))
Cheers
Robert
To answer your next question, Ruby’s objects have types, Ruby
variables do not. A variable’s type can change at any time:
x = []
=> []
x.class
=> Array
x = 42
=> 42
x.class
=> Fixnum
So, @installedPackageList was an Array when you created it with @installedPackageList = [] but then was re-assigned to a completely
different type with the call to readlines().
In languages like Java, if you do this:
int x = 50;
x = 40;
The first value of x is 50, but that value is completely thrown away
when you assign 40 to x. That’s what happened in the example, but the
type and value of the variable were changed.
@installedPackageList is an IO object, not an array. You need to read
its contents, convert it into an array, and then remove things
(readlines does steps one and two at once): @InstalledPackageList = @InstalledPackageList.readlines[5…-1]
daniel@daniel-desktop:~/lightconsolegem$ ri IO#readlines
----------------------------------------------------------- IO#readlines
ios.readlines(sep_string=$/) => array
Reads all of the lines in ios, and returns them in anArray. Lines
are separated by the optional sep_string. If sep_string is nil,
the rest of the stream is returned as a single record. The stream
must be opened for reading or an IOError will be raised.
f = File.new("testfile")
f.readlines[0] #=> "This is line one\n"
Dan
Hi all,
and thanks for your fast reply!
Sorry for the typo in the class name (before doing the post, I quickly
rewrote the code to make it more sane/readable).
I’m just a little bit confused. I thought that I declared
installedPackageList as an array in the constructor, and therefore
expected it to be used like an array.
Is that a typical pitfall to ruby newbies? I think I’ll have to use
reflection alot more.
Cheers
Joey
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.