Two dimention arrays and accessing syslog

All,

A newby question, only been using Ruby for about 2 days.

Is there a better way of creating a two dimention array, than the code
used
below

def create_world x, y
row = Array.new

y.times {
|j|
col = Array.new
x.times {
|i|

  map_type = rand(2)
  col[i] = map_type
}
row[j] = col

}

return row
end

world = create_world(gets.to_i, gets.to_i)

Also, what is the best method of accessing the syslog file within Linux?
I
could open the file for reading, but I want to make sure there isn’t a
better method first.

Regards
Sean Murphy
Skype: smurf20005

Life is a challenge, treat it that way.

Alle lunedì 14 gennaio 2008, Sean Murphy ha scritto:

y.times {

return row
end

world = create_world(gets.to_i, gets.to_i)

This should work:

def make_2d_array n_row, n_col
Array.new(n_row) do
Array.new(n_col){ rand(2) }
end
end

make_2d_array(gets.to_i, gets.to_i)

The block form of Array.new creates an array of the size passed as
argument,
then calls the block for each index of the array and stores the value
returned by the block in the corresponding element of the array (the
block
can take one parameter, the index of the element, which wasn’t necessary
in
this case). See the ri documentation for Array.new for more information.

I hope this helps

Stefano

Hi,

thanks for that, that was really cool code.

Regards
Sean Murphy
Skype: smurf20005

Life is a challenge, treat it that way.
----- Original Message -----
From: “Stefano C.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Monday, January 14, 2008 7:40 PM
Subject: Re: Two dimention arrays and accessing syslog.

Alle lunedì 14 gennaio 2008, Sean Murphy ha scritto:

}

return row
end

world = create_world(gets.to_i, gets.to_i)

This should work:

def make_2d_array n_row, n_col
Array.new(n_row) do
Array.new(n_col){ rand(2) }
end
end

make_2d_array(gets.to_i, gets.to_i)

The block form of Array.new creates an array of the size passed as
argument,
then calls the block for each index of the array and stores the value
returned by the block in the corresponding element of the array (the
block
can take one parameter, the index of the element, which wasn’t necessary
in
this case). See the ri documentation for Array.new for more information.

I hope this helps

Stefano

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs