0

I don't know how to explain this more, I'm open to suggestions.

Here is an example:

User.last.orders.detect { |user_order| user_order.query.id == 107 }

This doesn't work;

NameError: undefined local variable or method ` user_order' for main:Object
from (pry):51:in `block in <main>'

But this works:

User.last.orders.detect do |user_order|
  user_order.query.id == 107
end

This is just a simple example. I really don't understand why this happens. Thanks!

2
  • As pointed out below, it's likely a whitespace issue with your code. Otherwise, the code looks fine. Commented Oct 31, 2017 at 9:47
  • Thanks, I figured it out now. Commented Oct 31, 2017 at 10:37

1 Answer 1

2

The problem is you have an additional "non-breaking space" between the pipe and user_order, that's why the message says:

NameError: undefined local variable or method ` user_order' for main:Object

Your actual variable is " user_order" not "user_order".

This commonly happens when pressing alt and space at the same time (on OSX), that's common when opening curly braces for "inline" blocks in Ruby.

Sign up to request clarification or add additional context in comments.

4 Comments

"user_order not user_order" - wat? these are the same, no?
SO strips whitespace it views as un-necessary. Took me a minute to see as well, but if you look at the original error message things become more obvious when you're looking for a leading whitespace char in the variable name
@SimpleLime: ah, indeed. Yes, must be some unusual unicode whitespace in the source code.
@SergioTulentsev : In such cases, it helps looking at the code with a hex editor. This helps to identify the culprit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.