Why does single `=` work in `if` statement?

后端 未结 4 1191
囚心锁ツ
囚心锁ツ 2021-01-18 02:28

This code is provided as an example in for use with devise and OmniAuth, it works in my project.

class User < ActiveRecord::Base
  def self.new_with_sessi         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 03:09

    Ruby doesn't care about types in conditionals, unlike Java. As long as the value is neither nil or false then it will pass.

    In your example you actually discriminate against nil: the if conditionnal ensures that data actually exists and isn't nil, so we can use it, assuming it's a hash. This is a common pattern in Ruby.

提交回复
热议问题