# Writing a breakable loop? If it gets a match it should stop

**URL:** <https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423>\
**Category:** Rails\
**Created:** [January 9, 2007, 1:48am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423 "2007-01-09T01:48:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 1:48am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/1 "2007-01-09T01:48:57Z")

</div>

I’m trying to write a function that will accept a rails URL, and see if  
it matches one of my Rights. Rights are just controller URLs, so I can  
manage access at the controller level (don’t need action level). What  
I’m trying to do is take the url passed, and see if its access  
controlled. If it is, the method should stop and return true, if not it  
will return false. This method is used to check when someone logs out if  
they can be redirected to where they are, or if they need to be  
redirected to the home URL, it protects from an infinite login loop.

```auto
# true if this is a protected url that would cause a login loop
def looper_url?(url)
  rights = Right.find(:all)
  for right in rights
  if url_for(right) == url
    return true
    exit
  end
end
```

This is what I came up with, it doesn’t work but it should be pretty  
enough to give you an idea of what I’m trying to do. I think the problem  
is just in my inner most if block, not sure how to stop the loop and  
return true.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:24am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/2 "2007-01-09T02:24:31Z")

</div>

W. andrew Loe iii wrote:

> ```auto
> # true if this is a protected url that would cause a login loop
> > def looper_url?(url)
> > rights = Right.find(:all)
> > for right in rights
> > if url_for(right) == url
> > return true
> > exit
> > end
> > end
> ```
> 
> This is what I came up with, it doesn’t work but it should be pretty  
> enough to give you an idea of what I’m trying to do.

What doesn’t work? This is exactly what the “return” statement is for.  
It immediately stops executing the method its in and returns the value.  
I tried this in irb and it had the desired effect

> > def foo  
> > 10.times do |i|  
> > puts i  
> > return i if i \> 5  
> > end  
> > end

> > a = foo  
> > 1  
> > 2  
> > 3  
> > 4  
> > 5  
> > 6

> > puts a  
> > 6

as you can see, the loop is terminated as soon i is greater than 5,  
since 7 8 and 9 are not printed. Perhaps your condition is never true?  
Try a simpler comparison to be sure that “url\_for(right) == url” isn’t  
the problem instead of the return value.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:26am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/3 "2007-01-09T02:26:09Z")

</div>

You could just do:  
def allowed\_access?(url)  
Right.find(:all).detect {|r| url\_for r == url}  
end

That would return nil if no match, otherwise it returns a right object.  
So this would work:  
if allowed\_access(“url string”)  
#do something  
end

On Jan 9, 10:48 am, “W. andrew Loe iii”

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:31am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/4 "2007-01-09T02:31:03Z")

</div>

W. andrew Loe iii wrote:

> I’m trying to write a function that will accept a rails URL, and see if  
> it matches one of my Rights. Rights are just controller URLs, so I can  
> manage access at the controller level (don’t need action level). What  
> I’m trying to do is take the url passed, and see if its access  
> controlled. If it is, the method should stop and return true, if not it  
> will return false. This method is used to check when someone logs out if  
> they can be redirected to where they are, or if they need to be  
> redirected to the home URL, it protects from an infinite login loop.
> 
> ```auto
> # true if this is a protected url that would cause a login loop
> > def looper_url?(url)
> > rights = Right.find(:all)
> > for right in rights
> > if url_for(right) == url
> > return true
> > exit
> > end
> > end
> ```
> 
> This is what I came up with, it doesn’t work but it should be pretty  
> enough to give you an idea of what I’m trying to do. I think the problem  
> is just in my inner most if block, not sure how to stop the loop and  
> return true.

How about this?

def looper\_url?(url)  
return Right.find(:all).include?(url)  
end

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:33am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/5 "2007-01-09T02:33:34Z")

</div>

> How about this?
> 
> def looper\_url?(url)  
> return Right.find(:all).include?(url)  
> end

Thats a much clearer version of exactly what I was trying to do. Thank  
you so much!

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:41am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/6 "2007-01-09T02:41:41Z")

</div>

W. andrew Loe iii wrote:

> > How about this?
> > 
> > def looper\_url?(url)  
> > return Right.find(:all).include?(url)  
> > end
> 
> Thats a much clearer version of exactly what I was trying to do. Thank  
> you so much!

Your welcome 🙂

Looking back at you code I am not sure if it will work as you have a  
call to “url\_for” in there, so the results will depend on what in in the  
rights array.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:46am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/7 "2007-01-09T02:46:19Z")

</div>

The more I look at it, it might be better to be able to run url\_for on  
the rights item then it is to get the format of the referer right.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:49am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/8 "2007-01-09T02:49:41Z")

</div>

def looper\_url?(url)  
rights = Right.find(:all)  
for right in rights  
return if url\_for(right) == url  
end  
end

is working as expected.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:45am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/9 "2007-01-09T02:45:20Z")

</div>

Andrew S. wrote:

> W. andrew Loe iii wrote:
> 
> > > How about this?
> > > 
> > > def looper\_url?(url)  
> > > return Right.find(:all).include?(url)  
> > > end
> > 
> > Thats a much clearer version of exactly what I was trying to do. Thank  
> > you so much!
> 
> Your welcome 🙂
> 
> Looking back at you code I am not sure if it will work as you have a  
> call to “url\_for” in there, so the results will depend on what in in the  
> rights array.

Yea, I’m tinkering with it right now. Request.referer is the url thats  
gettting passed, but the “rights” are stored in the database as just the  
controller name, not a complete url. I am trying if  
request.referer.paramaters will work, or I’ll have to just explode and  
do it by hand.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 2:57am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/10 "2007-01-09T02:57:40Z")

</div>

W. andrew Loe iii wrote:

> The more I look at it, it might be better to be able to run url\_for on  
> the rights item then it is to get the format of the referer right.

That would be a better option int he long run as you most likely have  
other controllers that will call this procedure. Best to keep it all in  
one place - dry and all.

Sorry to get your hopes up 😛 Glad to see you have it working.

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [January 9, 2007, 3:10am UTC](https://www.ruby-forum.com/t/writing-a-breakable-loop-if-it-gets-a-match-it-should-stop/81423/11 "2007-01-09T03:10:13Z")

</div>

Hi –

On Tue, 9 Jan 2007, W. andrew Loe iii wrote:

> def looper\_url?(url)  
> rights = Right.find(:all)  
> for right in rights  
> return if url\_for(right) == url  
> end  
> end
> 
> is working as expected.

If you want to make it more concise, in the style of Andrew’s  
examples, you could do something like:

def looper\_url?(url)  
Right.find(:all).any? {|right| url\_for(right) == url }  
end

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
