0

I'm using rails console to debug an issue. What works with the rails server errors in rails console and I can not figure out why.

My code:

email = "team@mysite.com"
user_rules = [/\+/, /\-/, /all/, /updates/, /team/]
user, domain = email.downcase.split('@')
user_rules.each   { |rule| return false if !user.match(rule).nil?   }

The last line errors with:

1.9.3-p125 :016 > user_rules.each { |rule| return false if !user.match(rule).nil?}
LocalJumpError: unexpected return
    from (irb):16:in `block in irb_binding'
    from (irb):16:in `each'
    from (irb):16

Any ideas why?

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • 1
    This is basically a dup of http://stackoverflow.com/questions/2325471/using-return-in-a-ruby-block. The short answer is that you don't want to return from a lambda like that, because it's trying to return from the enclosing function. – Jim Stewart Jan 24 '13 at 00:59
  • @JimStewart thanks for pointing that out. Would would be the suggested update? Answer below and I'll mark it as correct. thanks – AnApprentice Jan 24 '13 at 01:01
  • 2
    To clarify, there's nothing explicitly wrong with the code, but it would probably be more clearly written as `return if user_rules.any? { |rule| !user.match(rule).nil? }`. – Jim Stewart Jan 24 '13 at 01:02
  • 3
    Or just `return if user_rules.any? {|rule| user.match(rule) }` – Andrew Haines Jan 24 '13 at 02:03

1 Answers1

1

You are using return false in

user_rules.each { |rule| return false if !user.match(rule) }

but lambda doesnot support return if not used inside function/method.

Babasaheb Gosavi
  • 7,735
  • 1
  • 14
  • 14