|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
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:
Can someone point out my error? I need the Dir.foreach to do something while the directory isnt "." or ".." Cheers - Karym6 |
|
#2
|
|||
|
|||
|
Quote:
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 Code:
while X or ".." For example: Code:
while ".." do puts "Still looping!" end 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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
Quote:
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 |
|
#6
|
|||
|
|||
|
Quote:
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] Last edited by L7Sqr : October 2nd, 2007 at 08:35 AM. |
|
#7
|
|||
|
|||
|
Quote:
thanks very much L7Sqr this is what I didnt get from the docs etc ![]() |
|
#8
|
|||
|
|||
|
Quote:
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 |
|
#9
|
|||
|
|||
|
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'] |
|
#10
|
|||
|
|||
|
Quote:
Excellent! Thankyou very much for your help so far ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > While not loop in ruby |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|