Add elment on the first index of an array

Hi.

I have an array. Now I will add a element on the first index. I do this
like this:

$a_foo = Array.new
$a_foo = $bar.split(’/’)
$a_foo = $a_foo.reverse
$a_foo << ‘something’
$a_foo = $a_foo.reverse

Is there a shorter way to do this?

greetings
Dirk Einecke

Use unshift
e.g.
irb(main):001:0> [1,2,3].unshift(0)
=> [0, 1, 2, 3]

Hth

···

On Sun, 18 Apr 2004 22:21:42 +0200, Dirk Einecke wrote:

Hi.

I have an array. Now I will add a element on the first index. I do this
like this:

$a_foo = Array.new
$a_foo = $bar.split(‘/’)
$a_foo = $a_foo.reverse
$a_foo << ‘something’
$a_foo = $a_foo.reverse

Is there a shorter way to do this?

greetings
Dirk Einecke

Dirk Einecke wrote:

Is there a shorter way to do this?

[‘something’] + $a_foo

$a_foo = $bar.split(‘/’).unshift(‘something’)

···

On Sun, 18 Apr 2004 22:21:42 +0200, Dirk Einecke wrote:

Hi.

I have an array. Now I will add a element on the first index. I do this
like this:

$a_foo = Array.new
$a_foo = $bar.split(‘/’)
$a_foo = $a_foo.reverse
$a_foo << ‘something’
$a_foo = $a_foo.reverse

Is there a shorter way to do this?

greetings
Dirk Einecke

Apart from anything else, note that this is unnecessary in Ruby -
objects are typed, but variables are not. So $a_foo = Array.new just
creates an empty array, and discards it in the next line - $bar.split
creates an entirely new array, to which $a_foo now points.

martin

···

Dirk Einecke dirk.einecke@gmx.de wrote:

Hi.

I have an array. Now I will add a element on the first index. I do this
like this:

$a_foo = Array.new
$a_foo = $bar.split(‘/’)

Hi Xavier.

Xavier wrote:

Use unshift
e.g.
irb(main):001:0> [1,2,3].unshift(0)
=> [0, 1, 2, 3]

Okay. Thank you. Works fine.

greetings
Dirk Einecke

irb(main):001:0> values=%w(a b c)
=> [“a”, “b”, “c”]
irb(main):002:0> values[0, 0] = 666
=> 666
irb(main):003:0> p values
[666, “a”, “b”, “c”]
=> nil
irb(main):004:0>

···

On Mon, 19 Apr 2004 01:44:50 +0200, Linus Sellberg wrote:

Dirk Einecke wrote:

Is there a shorter way to do this?

[‘something’] + $a_foo


Simon Strandgaard

Kansas 0.0.3 has been released.

Kansas is an object/relational mapping layer. It takes a relational
schema, accessible via DBI, and converts it into a set of Ruby classes.
It is simple, lightweight, and easy to use.

This release supports a larger variety of query types, including sorting
and limits as well as a number of common database functions. It now
supports inserts and deletes along with commit/rollback for transactions.

It can be found at:

http://rubyforge.org/projects/kansas

A partial tutorial (also expanded and a couple errors fixed since the
kansas 0.0.1 release) is available at:

http://enigo.com/projects/kansas

It has also been requested that there be a mailing list for Kansas, so
kansas-general@rubyforge.org is now available.

Thanks much. If you check it out, let me know if you have any questions
or problems.

Kirk Haines