Should I use if defined?
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
Additionally, the nicer ||=
produces a warning (on 1.8.6 and 1.8.7, at least) about uninitialized instance variables, while the more verbose defined?
version does not.
On the other hand, this probably does what you want:
def initialize
@foo = nil
end
def foo
@foo ||= some_long_calculation_for_a_foo
end
But this almost certainly does not:
def initialize
@foo = nil
end
def foo
return @foo if defined?(@foo)
@foo = some_long_calculation_for_a_foo
end
since @foo
will always be defined at that point.