# How to write in an array inside a block?

**URL:** <https://www.ruby-forum.com/t/how-to-write-in-an-array-inside-a-block/185806>\
**Category:** Ruby\
**Created:** [March 26, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/how-to-write-in-an-array-inside-a-block/185806 "2010-03-26T13:44:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zathras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zathras](https://www.ruby-forum.com/u/zathras)\
**Post date:** [March 26, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/how-to-write-in-an-array-inside-a-block/185806/1 "2010-03-26T13:44:01Z")

</div>

Hello,

i want to write all files from my local directory in an array.

Something like (not working):

d = Dir.new("/Volumes/System/")  
d.each {|x| file\_list.push(x) }

How can i push single filenames through d.each into an array?

Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![zathras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zathras](https://www.ruby-forum.com/u/zathras)\
**Post date:** [March 26, 2010, 2:35pm UTC](https://www.ruby-forum.com/t/how-to-write-in-an-array-inside-a-block/185806/2 "2010-03-26T14:35:32Z")

</div>

On Mar 26, 8:44 am, Zathras D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks a lot in advance!
> 
> Posted viahttp://www.ruby-forum.com/.

If file\_list did not exist before the block,  
it will not exist after the block exits. Declarations inside blocks  
are scoped to the block

I think if you look at the docs for Dir you’ll find a method that will  
give you the list of files withtout the need of the block  
cheers

---

<div class="post-metadata">

**Author:** ![zathras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zathras](https://www.ruby-forum.com/u/zathras)\
**Post date:** [March 26, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/how-to-write-in-an-array-inside-a-block/185806/3 "2010-03-26T14:44:52Z")

</div>

Chris H. wrote:

> > On Mar 26, 8:44ï¿½am, Zathras D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > Hello,
> > 
> > i want to write all files from my local directory in an array.
> 
> If file\_list did not exist before the block,  
> it will not exist after the block exits. Declarations inside blocks  
> are scoped to the block
> 
> I think if you look at the docs for Dir you’ll find a method that will  
> give you the list of files withtout the need of the block  
> cheers

Indeed.

irb\> file\_list = Dir.glob(‘\*’)
