Deleting first lines of Array

J

Jo Ay

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:

Regards
Joey


#############################################################

#!/usr/bin/ruby

class dpkg
def initialize
@InstalledPackageList = Array.new
end

def collect_inst_packages
@InstalledPackageList = `/usr/bin/dpkg -l`.split( /\n/ )
@InstalledPackageList.slice!(0, 5)
end

def get_inst_packages(filter = "")
@InstalledPackageList.grep(/#{filter}/)
end

def show_inst_packages(filter = "")
puts @InstalledPackageList.grep(/#{filter}/)
end
end

ax = dpkg.new
ax.collect_inst_packages
ax.show_inst_packages(ARGV[0])

#############################################################


# 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

#############################################################
 
M

Marc Heiler

Question: what am I doing wrong?

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
 
D

Daniel Finnie

Hi,

@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
 
D

David A. Black

Hi --

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).


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!
 
J

Jo Ay

Daniel said:
Hi,

@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).

@Dan: that was the solution to my problem!

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
 
R

Robert Dober

Hi --


<snip>
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).

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 http://www.rubyquiz.com/quiz67.html)
Cheers
Robert
 
D

Daniel Finnie

Anytime.

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.

Dan

Daniel said:
Hi,

@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).

@Dan: that was the solution to my problem!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top