The only thing that will happen differently is if an exception is raised. For instance, let's say there is a problem in the config_exists?
method call. If it raises an exception in the first example your @config
var will be set to {}
. In the second example if the same thing happens your program will crash.
As a side note, there is no need for the return
keyword here. In fact the example should read as follows. This is assuming that I understand the intent.
def config
@config ||=
begin
if config_exists?
some_value
else
{}
end
rescue
{}
end
end
and
def config
@config ||= method
end
def method
if config_exists?
some_value
else
{}
end
end
Both examples are exactly the same, except if an exception is raised @config
will still be set to = some_value
in the first example.
Also, it should be noted that nothing will happen if @config
already has a value. The ||=
operators is the same as:
@config = some_value if @config.nil?
Only set the variable to this value if it is currently nil.
Hope this is helpful and that I am understanding your question correctly.