routes.each do |route|
result = route.recognize(path, environment) and return result
end
I didn't understand this block at all. So I posted this in the Ruby mailing list. The good folks at the group answered me back immediately with some good examples. Here is the link:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/877ad90d98f02eb2/b8195327bb522ac4#b8195327bb522ac4
To summarize:
If the first operand (we can even call it a statement), i.e. result = route.recognize(path, environment) retuns true then the following things would happen:
- 'result' variable contains the result of the recognize statement (which is a hash)
- The second operand (statement) gets evaluated which in turn ends the block by returning the result variable
But what is tricky in the statement "result = true and x = 20" is that 'result' contains true and not 20. Which means the first operand is also doing an assignment. I initially thought result will contain 20 because that's return value of the second statement.
Here is another explanation of the same subject:
http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html
Super stuff. Ruby Rocks.
No comments:
Post a Comment