Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > read a ruby script like you would read a text file

Reply
Thread Tools

read a ruby script like you would read a text file

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      01-27-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-27-2009
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?

> 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.


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

--
remember.guy do |as, often| as.you_can - without end
 
Reply With Quote
 
 
 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      01-27-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If you could change the C or C++ or Java syntax, what would you like different? Alexander C++ 68 11-08-2013 01:23 PM
don't like, so don't waste your time thinking and buy them. You know,if you don't like one thing, it means nothing for you.</p> fashion t shirts seller Cisco 0 06-13-2011 02:01 AM
2011: Which Ruby books have you read? And which would you recommend? Aston J. Ruby 13 03-16-2011 07:36 AM
If you could change the C or C++ or Java syntax, what would you like different? Alexander C Programming 620 11-09-2010 10:31 PM
Incredible - I bet you would like a camera like this! ~BD~ Computer Security 1 02-26-2009 09:45 PM



Advertisments