# Two dimention arrays and accessing syslog

**URL:** <https://www.ruby-forum.com/t/two-dimention-arrays-and-accessing-syslog/125961>\
**Category:** Ruby\
**Created:** [January 14, 2008, 9:15am UTC](https://www.ruby-forum.com/t/two-dimention-arrays-and-accessing-syslog/125961 "2008-01-14T09:15:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Murphy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@Sean\_Murphy](https://www.ruby-forum.com/u/Sean_Murphy)\
**Post date:** [January 14, 2008, 9:15am UTC](https://www.ruby-forum.com/t/two-dimention-arrays-and-accessing-syslog/125961/1 "2008-01-14T09:15:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sean\_Murphy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@Sean\_Murphy](https://www.ruby-forum.com/u/Sean_Murphy)\
**Post date:** [January 14, 2008, 9:41am UTC](https://www.ruby-forum.com/t/two-dimention-arrays-and-accessing-syslog/125961/2 "2008-01-14T09:41:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sean\_Murphy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@Sean\_Murphy](https://www.ruby-forum.com/u/Sean_Murphy)\
**Post date:** [January 14, 2008, 10:01am UTC](https://www.ruby-forum.com/t/two-dimention-arrays-and-accessing-syslog/125961/3 "2008-01-14T10:01:12Z")

</div>

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.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
