{"comment":{"created_at":"2011-10-09T17:35:09Z","body":"You can avoid accidental assignments if you use yoda conditions.\r\n\r\n```ruby\r\nif v = array.grep(/foo/)\r\n  # assignment\r\nend\r\n\r\nif array.grep(/foo/) == v\r\n  # conditional\r\nend","updated_at":"2011-10-09T17:35:09Z","url":"https://api.github.com/repos/bbatsov/ruby-style-guide/issues/comments/2337472","id":2337472,"user":{"avatar_url":"https://secure.gravatar.com/avatar/58fc8eaee4847fe3d7ebae147d43c32e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/asifkalam","id":98596,"login":"asifkalam"}},"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":2,"title":"Using assignment in an if statement","updated_at":"2011-10-09T17:35:09Z","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":2337472}}