# Read a ruby script like you would read a text file

**URL:** <https://www.ruby-forum.com/t/read-a-ruby-script-like-you-would-read-a-text-file/158763>\
**Category:** Ruby\
**Created:** [January 27, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/read-a-ruby-script-like-you-would-read-a-text-file/158763 "2009-01-27T19:48:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 27, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/read-a-ruby-script-like-you-would-read-a-text-file/158763/1 "2009-01-27T19:48:24Z")

</div>

Do you know if this is possible:  
I want to read each ruby script in a directory (btw there a hundreds of  
them) and then use that info to lable and make reports later. For now, I  
just want to know if there is a possiblity to do this.

Each script contains an intialized setting that I can use to distinguish  
the difference in my file standards. Later, I am going to create a  
report based on what is available. Any suggestions for the below code?  
Thanks, MC

Dir.entries(“C:/ScriptDirectory”).each do |filename|  
if File.extname(filename) == “.rb” then #read a ruby file  
File.open(filename, ‘r’) do |f1|  
f1.each\_line |line|  
@fileStandard1 \<\< filename if line =~ /setting343234/  
puts “Stardard1” +@fileStandard1  
@fileStandard2 \<\< filename if line =~ /setting343999/  
puts “Standard2” +@fileStandard2  
@fileStandard3 \<\< filename if line =~ /setting343245/  
puts “Standard3” +@fileStandard3  
end  
end  
end

##this code is a snippet I am going to use the new variables later in  
the program.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 27, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/read-a-ruby-script-like-you-would-read-a-text-file/158763/2 "2009-01-27T23:40:31Z")

</div>

On 27.01.2009 19:46, Mmcolli00 Mom wrote:

> Do you know if this is possible:  
> I want to read each ruby script in a directory (btw there a hundreds of  
> them) and then use that info to lable and make reports later. For now, I  
> just want to know if there is a possiblity to do this.

What do you mean by “read”? Do you want to execute the Ruby code in  
those scripts?

> ```
> @fileStandard1 << filename if line =~ /setting343234/
> 
> ```
> 
> the program.  
> There seems to be a bit of redundant code. How about

@file\_standards = {  
343234 =\> [],  
343999 =\> [],  
343245 =\> [],  
}

Dir[“C:/ScriptDirectory/\*.rb”].each do |file|  
File.foreach file do |line|  
num = line[/settings(\d+)/, 1].to\_i  
std = @file\_standards[num] and std \<\< file  
end  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 27, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/read-a-ruby-script-like-you-would-read-a-text-file/158763/3 "2009-01-27T23:55:00Z")

</div>

Thanks Robert,

Yes, I didn’t want to run the scripts only read through them and  
categorize them. This really did the trick. Pretty fancy and efficient  
too! 🙂

Thanks, MC
