Aaron
Aaron

Reputation: 359

If statement not catching nil

I have the following function:

def price
  if super == nil
    return 'super'
  end
  super
end

nil is being returned. How is this possible? Should the if statement catch super if it equals nil and then return 'super'

Upvotes: 0

Views: 156

Answers (2)

tadman
tadman

Reputation: 211540

That's a very strange way of saying something that should be expressed more simply as:

def price
  super || 'super'
end

It might be the case that the second call to the same method returns nil for some reason. You're not capturing the initial result so it's hard to say why this isn't working.

Upvotes: 1

Matt
Matt

Reputation: 678

If super can return different values each time, this is possible (if unlikely). Perhaps try the following? It only calls super once, so it doesn't have that pitfall.

def price
  super || 'super'
end

Upvotes: 2

Related Questions