Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2007, 05:35 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
While not loop in ruby

Hi All,

I am trying to write a while not loop in Ruby, I have taken a look at all the docs etc on control structures but cant quite nail this one...

Basically, I want to do something like this:

Code:
while Dir.entries("c:\\preprocess") != "." or ".."{
Dir.foreach("c:\\preprocess") {|sitename|system("c:\\releasescripts\\aspincluder.exe c:\\preprocess #{sitename}")} 
} end


But I keep getting:
DOS Code:
Original - DOS Code
    C:/dir_test.rb:34: syntax error, unexpected '{', expecting kDO_COND or ':' or '\ n' or ';' C:/dir_test.rb:36: syntax error, unexpected '}', expecting $end } end  ^


Can someone point out my error? I need the Dir.foreach to do something while the directory isnt "." or ".."

Cheers
-
Karym6

Reply With Quote
  #2  
Old October 2nd, 2007, 07:00 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 647 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 50 m 19 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
Quote:
while Dir.entries("c:\\preprocess") != "." or ".."

So you are aware, the logic of this statement is wrong, so the intended control is not actually what you would get.
When you say:
Code:
while cond1 or cond2
You are saying that either cond1 or cond2 evaluating to non nil/false will allow the loop to happen. So in your specific statement, you are saying
Code:
while X or ".."
where X could be anything as ".." will always allow the loop to be entered.
For example:
Code:
while ".." do
   puts "Still looping!"
end
will produce
Code:
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
Still looping!
...


To answer your question directly, you could change the loop construct to use an iterator and only use items that qualify...
Code:
Dir.entries("c:\\preprocess").each do |entry|
   next if entry =~ /^\./
   # now any filename that begins with a '.' will not reach this line
end

That will eliminate 'hidden' files as well as simply '..' and '.', so you may have to adjust the reg ex to match against to suit your needs.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old October 2nd, 2007, 07:14 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
Thanks for the reply L7Sqr!

does this mean then, if I wished to exclude further directories that did not start with a "." I could use:

Code:
Dir.entries("c:\\preprocess").each do |entry|
   next if entry =~ /^\./ or "some_dir"
end


Or have I totally lost track?

Cheers
-
K

Reply With Quote
  #4  
Old October 2nd, 2007, 07:31 AM
[H4z3] [H4z3] is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: /usr/bin/ruby
Posts: 63 [H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 32 m 36 sec
Reputation Power: 23
Code:
next if entry =~ /^\./ or entry =~ /\.txt$/

Will skip files beginning with a dot and ending in .txt, don't forget that the entry variable is each file name

Last edited by [H4z3] : October 2nd, 2007 at 07:37 AM.

Reply With Quote
  #5  
Old October 2nd, 2007, 08:05 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
Quote:
Originally Posted by [H4z3]
Code:
next if entry =~ /^\./ or entry =~ /\.txt$/

Will skip files beginning with a dot and ending in .txt, don't forget that the entry variable is each file name


ahh, I think I see now.

So in order to ignore a whole directory as opposed to a file i can do:

Code:
next if entry =~ /^\./ or entry =~ /\some_dir$/


cheers
-
K

Reply With Quote
  #6  
Old October 2nd, 2007, 08:31 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 647 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 50 m 19 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
Quote:
So in order to ignore a whole directory as opposed to a file i can do:
The result of Dir.entries will be an array of strings. So there is no idea of file/directory at that point.
If you have a list of items you would like to exclude, there is an easier method that you may be interested in.
Code:
# items to ignore
ignored = ['.', '..', 'some_dir']
(Dir.entries("c:\\preprocess") - ignored).each do |entry|
   # no items in ignored list will be present here
end


[edit]
Also, if you are looking to only process files (and ignore all directories) then you can use test
Code:
Dir.entries("c:\\preprocess").each do |entry|
   next unless test ?f, entry
   # or, next if test ?d, entry
   # depends on what you want to limit
end

[/edit]
Comments on this post
karym6 agrees: very helpful

Last edited by L7Sqr : October 2nd, 2007 at 08:35 AM.

Reply With Quote
  #7  
Old October 2nd, 2007, 08:39 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
Quote:
Originally Posted by L7Sqr
The result of Dir.entries will be an array of strings. So there is no idea of file/directory at that point.
If you have a list of items you would like to exclude, there is an easier method that you may be interested in.
Code:
# items to ignore
ignored = ['.', '..', 'some_dir']
(Dir.entries("c:\\preprocess") - ignored).each do |entry|
   # no items in ignored list will be present here
end


[edit]
Also, if you are looking to only process files (and ignore all directories) then you can use test
Code:
Dir.entries("c:\\preprocess").each do |entry|
   next unless test ?f, entry
   # or, next if test ?d, entry
   # depends on what you want to limit
end

[/edit]


thanks very much L7Sqr this is what I didnt get from the docs etc

Reply With Quote
  #8  
Old October 2nd, 2007, 09:49 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
Quote:
Originally Posted by karym6
thanks very much L7Sqr this is what I didnt get from the docs etc


one last question - would I be able to include specific directories with this code?

Code:
# items to ignore
ignored = ['.', '..', 'some_dir']
(Dir.entries("c:\\preprocess") - ignored).each do |entry|
   # no items in ignored list will be present here
end


is the - ignored the key part here?

Cheers
-
Karim

Reply With Quote
  #9  
Old October 2nd, 2007, 09:03 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 647 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 50 m 19 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
Yes. As I mentioned, the 'entries' method returns an array of strings.
You are free to add (+) and remove (-) from as you see fit.
As an example of adding and removing from an array:
Code:
a = ['a', 'b', 'c', 'd', 'e']
b = ['d', 'e', 'f', 'g', 'h']
a - b # produces ['a', 'b', 'c']
a + b # produces ['a', 'b', 'c', 'd', 'e', 'd', 'e', 'f', 'g', 'h']
Notice the duplicates in teh second result.

Reply With Quote
  #10  
Old October 3rd, 2007, 07:36 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 75 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 8 m 59 sec
Reputation Power: 5
Send a message via MSN to karym6
Quote:
Originally Posted by L7Sqr
Yes. As I mentioned, the 'entries' method returns an array of strings.
You are free to add (+) and remove (-) from as you see fit.
As an example of adding and removing from an array:
Code:
a = ['a', 'b', 'c', 'd', 'e']
b = ['d', 'e', 'f', 'g', 'h']
a - b # produces ['a', 'b', 'c']
a + b # produces ['a', 'b', 'c', 'd', 'e', 'd', 'e', 'f', 'g', 'h']
Notice the duplicates in teh second result.


Excellent! Thankyou very much for your help so far

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > While not loop in ruby


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |