-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about if a = b.first
snippet in README
#1469
Comments
The condition was actually supposed to be `if geo = results.first`, because that's not always obvious the intention and fails lint, I'm just doing a truthy check for it which should be the same.
Interesting question. As far as I know, this is standard Ruby style. I've been using it for more than a decade. Possibly I've gotten old and my style has become dated. The statement will return false if Put another way, the line in question checks whether Does that help? |
Hi @alexreisner, thanks for the answer. Yes, that does help. It was also pointed out/explained to me in another thread that Between your answer and the comment I just linked, I'm able to see why the snippet works. Thanks again for taking the time. (Whether to follow Rubocop in this case, I'll just keep that as being a decision over at the repo I'm helping to maintain.) |
Hello,
I'm maintaining a project that uses
geocoder
, and we've apparently copy-pasted a snippet fromREADME.md
some time ago. I'm also a bit of a beginner with Ruby, admittedly. The snippet doesn't make a lot of sense to me, and I'm wondering if this is intentional, or there's a special purpose to the code pattern? Any explanation you can give is appreciated.in
README.md
there is the following snippet (https://github.com/alexreisner/geocoder/tree/v1.6.4#custom-result-handling)Particularly this line is confusing me:
As I understand it, this attempts to assign
results.first
to the (potentially newly created)geo
object. And if this works, proceeds to do the content of theif/end
block.My questions are:
if a = b
-->if a == b
?)geo
and do anif
statement on the same line? This happens to fail the default Rubocop linting.geo
is non-empty?geo = results.first
followed by (separately, on the next line)if geo # [then do stuff]
irb
, assigning from an "undefined local variable or method" gives aNameError
. Theif
statement here is eithertrue
or there is a program error, right?if
statement at all, if the alternative situation isn't skipping the content of theif/end
block, but rather erroring out of the program?I may be deeply misunderstanding this, but a project I work on has this snippet copy-pasted in, and we're trying to enable Rubocop. This snippet has me scratching my head a bit.
Sorry to take your time, but any explanation would be much appreciated as I try to figure out what this code is doing. Thanks for considering.
Best regards,
- DeeDeeG
P.S. this line appears to date all the way back to April 2011: 54329e5 (exact line) Not sure if that context helps, but I thought I mention that anyway.
The text was updated successfully, but these errors were encountered: