What is the significance of the leading trailing underscores with instance variable names?

Hi,

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.

–Brian Kernighan

Sorry a bit change — I meant to say, leading two underscores, not
trialing, like @_current_user

On Sep 6, 2014, at 5:50, Arup R. [email protected]
wrote:

def current_user
@_current_user ||= session[:current_user_id] &&

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.

On 14-09-06, 1:49, Arup R. wrote:

link :

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.

Andrew V.

Please Arup - do not pick up bad habits.

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?

thanks

On 6 Σεπ 2014, at 20:01 , Andrew V. [email protected] wrote:

Andrew V.

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

“As you set out for Ithaca, hope the voyage is a long one, full of
adventure, full of discovery […]” - C. P. Cavafy

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.

Yeah, but…

return @connection.primary_key(table) if
@connection.respond_to?(:primary_key)
nil
end

is the same as

if @connection.respond_to?(:primary_key)
return @connection.primary_key(table)
end
nil

but “if” evaluates to nil if the condition is false…

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”.

  • A

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.

Hope this helps,

Jesus.

On 8 September 2014 22:54, Alex C. [email protected] wrote:

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.​

Robert H. wrote in post #1157020:

Please Arup - do not pick up bad habits.

What did you mean ? :slight_smile:

Jesús Gabriel y Galán wrote in post #1157121:

A method returns the result of the last expression,

Yes. So what is the “result” of

if false
end

??

result = if 1.nil?
45
end

p result #=> nil