I am reading Rails guide, and I found the below code -
class ApplicationController < ActionController::Base
private
Finds the User with the ID stored in the session with the key
:current_user_id This is a common way to handle user login in
a Rails application; logging in sets the session value and
logging out removes it.
def current_user @_current_user ||= session[:current_user_id] &&
User.find_by(id: session[:current_user_id])
end
end
link :
What is the significance of the leading trailing underscores with
instance
variable names ?
–
Regards,
Arup R.
Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.
What is the significance of the leading trailing underscores with instance
variable names ?
In this case, it’s to signal that @_current_user is an
implementation detail, and shouldn’t be used directly (i.e. only
through the current_user method).
In other cases, I’ve seen it used to indicate that a variable is
extracted from arguments or an array or structure, but never used.
What is the significance of the leading trailing underscores with instance
variable names ?
ActionController takes all assigned instance variables (in web action
methods, defined in subclasses) and exposes them to the view context.
The underscore is there to signal that this variable is not meant to be
accessed outside the controller context. I think it would still be
accessible in the view, but you’d have to knowingly use the underscore:
it’s a signal that you shouldn’t.
On Mon, Sep 8, 2014 at 12:28 PM, Panagiotis A. [email protected] wrote:
Hello,
Since we’re on topic about underscore what is the meaning of ‘_’ here:
in lines 113 and 114
he returns a value, then types ‘nil’. Why is that? If the value is not returned
isn’t nil or error anyway?
def primary_key_for(table)
if @connection.respond_to?(:pk_and_sequence_for)
pk, _ = @connection.pk_and_sequence_for(table)
return pk if pk
end
return @connection.primary_key(table) if @connection.respond_to?(:primary_key)
nil
end
In line 110, the underscore is a convention to say that you are not
interested in that value. As the method pk_and_sequence_for returns
two values and you are only interested in the first one, you don’t
have to invent a variable name to not use it, this convention was
created for that.
Regarding the return and the nil, the return contains an if clause. So
it will only actually return if the @connection.respond_to? method
returns true. Otherwise, the return will not be executed, and the next
line is evaluated. A method returns the result of the last expression,
and in this case, if the code reaches that line, the last expression
will evaluate to nil, and that’s what the method will return.
returns true. Otherwise, the return will not be executed, and the next
end
if @connection.respond_to?(:primary_key)
return @connection.primary_key(table)
else
nil
end
nil
So that final nil is redundant. And so is the “return”.
Saying it returns nil doesn’t necessarily mean it returns nil, but it
definitely means don’t use the return value.