# Deleting first lines of Array

**URL:** <https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681>\
**Category:** Ruby\
**Created:** [April 14, 2008, 12:29am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681 "2008-04-14T00:29:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 12:29am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/1 "2008-04-14T00:29:30Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 12:56am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/2 "2008-04-14T00:56:37Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 1:05am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/3 "2008-04-14T01:05:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 1:42am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/4 "2008-04-14T01:42:14Z")

</div>

Hi –

On Mon, 14 Apr 2008, Marc H. wrote:

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

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

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/5 "2008-04-14T23:19:06Z")

</div>

On Mon, Apr 14, 2008 at 1:41 AM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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)](http://www.rubyquiz.com/quiz67.html))  
> Cheers  
> Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 15, 2008, 1:50am UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/6 "2008-04-15T01:50:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@javent](https://www.ruby-forum.com/u/javent)\
**Post date:** [April 14, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/deleting-first-lines-of-array/134681/7 "2008-04-14T22:56:27Z")

</div>

Daniel F. wrote:

> 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
