{"comment":{"created_at":"2011-10-07T19:30:31Z","body":"I like this idea...\r\n\r\nBesides, the parentheses are often necessary because the boolean operators have a higher precedence than the assignment operator, e.g.:\r\n\r\n```ruby\r\nif v = \"hello\" == \"hello\"\r\n  puts v # => true\r\nend\r\n\r\nif (v = \"hello\") == \"hello\"\r\n  puts v # => hello\r\nend\r\n```\r\n\r\nI'm curious to see what other (and more experienced) programmers think about this.","updated_at":"2011-10-07T19:30:31Z","url":"https://api.github.com/repos/bbatsov/ruby-style-guide/issues/comments/2326837","id":2326837,"user":{"avatar_url":"https://secure.gravatar.com/avatar/502f0e339556081f3541bba744edeb50?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/philtr","id":30456,"login":"philtr"}},"action":"created","issue":{"number":53,"created_at":"2011-10-04T00:17:12Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"One of the reasons people don't like using assignment within if statements is because of how common it is for\r\n\r\nThere's a convention I've used to make it clear that the assignment is intentional:\r\n\r\n```ruby\r\n  # Intentional assigment\r\n  if (v = array.grep(/foo/))\r\n    ...\r\n  end\r\n\r\n  # Oops, this should be equality\r\n  if v = 1\r\n    ...\r\n  end\r\n```\r\n\r\nI like this because it sort of separates the assignment from the conditional, and clarifies the intention.\r\n\r\nWhat do y'all think?\r\n","comments":1,"title":"Using assignment in an if statement","updated_at":"2011-10-07T19:30:31Z","url":"https://api.github.com/repos/bbatsov/ruby-style-guide/issues/53","id":1809214,"assignee":null,"milestone":null,"closed_at":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/717f2120e2cacda5fd3bf45232f04e28?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/amiel","id":27160,"login":"amiel"},"html_url":"https://github.com/bbatsov/ruby-style-guide/issues/53","labels":[],"state":"open"},"legacy":{"issue_id":1809214,"comment_id":2326837}}