8

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_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end 

I don't know why it's a single equals sign as apposed to a double equals sign, which I thought was necessary for if-statements. My IDE "intelliJ IDEA" agrees with my concerns.

5 Answers 5

9

The only necessary thing for an if statement to be valid is a boolean expression. In this case, since = returns the result of the assignment, what's actually being tested is the falsiness of session["devise.facebook_data"].

IntelliJ has a good point to lodge a complaint about code like this, as it's difficult to read without knowing a thing or two about Ruby. A recommendation would be to move that to an explicit assignment statement instead. This has the added benefit of DRYing up a reference to it twice.

class User < ActiveRecord::Base def self.new_with_session(params, session) super.tap do |user| data = session["devise.facebook_data"] if data && data["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!! Makes sense now, you explained that perfectly.
Not sure what you mean by "necessary thing ... to be valid is a boolean expression." Any object can appear after if. Is there any "non-boolean" object that you have in mind that would make an if condition illegal?
@sawa When I'm near a machine I can revise that part.
2

In Ruby, a single equals sign is used for assignment. The expression

data = session["devise.facebook_data"] 

assigns the result of evaluating session["devise.facebook_data"] to a local variable named data.

If the session hash doesn't have a "devise.facebook_data" key, it will return nil and data will be assigned nil. Assignments evaluate to the value being assigned, so the assignment will evaluate to nil as well. nil is considered falsey in a boolean context, so the right operand of the && will not be evaluated. That way, you won't get a NoMethodError trying to call nil["extra"]["raw_info"].

If the session hash does have a "devise.facebook_data" key, data will be set to the value associated with it. Any value other than nil and false is considered truthy, therefore the right-hand operand of the && operator will be evaluated.

If the condition is truthy, the then clause will be evaluated, which uses the data variable assigned in the condition.


Note: I believe one could also use the data variable within the right-hand side of the && operator, i.e. the condition could read like this instead:

if data = session["devise.facebook_data"] && data["extra"]["raw_info"] 

But I'll have to check that.

2 Comments

Yea thats what i thought, I guess im just confused by the layout of the function. Could you possibly explain what exactly the if is asking? I see the &&, but it seems like theres more than that?
The first if is equivalent to data = ... followed by if data; user.email = ....
1

A assignment operator (=) returns the assigned value, which is then evaluated by the if. In ruby, only false and nil are considered as false. Everything else evaluates to true in a boolean context (like an if).

Comments

0

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.

1 Comment

Actually, whether or not a value is falesy or not is determined by its type (class), i.e., if it is an instance of NilClass or FalseClass, then it is falesy.
-1

Ruby returns the value when an assignment is made:

foo = data = 'Chalupa' puts "foo value #{foo}" foo value Chalupa 

this can be used as a "trick" to verify that some code is executed only when the assignment was successful because you can use the new variable inside the if "block":

if klass = Object.const_get('String') rescue nil puts "The class indeed exists: #{klass.class}" end 

but as mentioned, this approach is not recommended since it's hard to read and likely Ruby has more robust and standard ways to be sure a truthy variable is in fact there, like a simple Guard Clause.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.