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

**URL:** <https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922>\
**Category:** Ruby\
**Created:** [September 6, 2014, 11:50am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922 "2014-09-06T11:50:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 6, 2014, 11:50am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/1 "2014-09-06T11:50:27Z")

</div>

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 :

> **[Action Controller Overview — Ruby on Rails Guides](https://guides.rubyonrails.org/action_controller_overview.html#accessing-the-session)**
>
> Action Controller OverviewIn this guide, you will learn how controllers work and how they fit into the request cycle in your application.After reading this guide, you will know: How to follow the flow of a request through a controller. How to...

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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 6, 2014, 11:57am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/2 "2014-09-06T11:57:40Z")

</div>

Sorry a bit change — I meant to say, leading **two underscores** , not  
trialing, like @\_current\_user

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 6, 2014, 1:35pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/3 "2014-09-06T13:35:13Z")

</div>

On Sep 6, 2014, at 5:50, Arup R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def current\_user  
> @\_current\_user ||= session[:current\_user\_id] &&

> 

> **[Action Controller Overview — Ruby on Rails Guides](https://guides.rubyonrails.org/action_controller_overview.html#accessing-the-session)**
>
> In this guide, you will learn how controllers work and how they fit into the request cycle in your application.After reading this guide, you will know how to: Follow the flow of a request through a controller. Access parameters passed to your...

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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 6, 2014, 8:02pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/4 "2014-09-06T20:02:41Z")

</div>

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

> link :

> **[Action Controller Overview — Ruby on Rails Guides](https://guides.rubyonrails.org/action_controller_overview.html#accessing-the-session)**
>
> In this guide, you will learn how controllers work and how they fit into the request cycle in your application.After reading this guide, you will know how to: Follow the flow of a request through a controller. Access parameters passed to your...

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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 7, 2014, 1:31am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/5 "2014-09-07T01:31:59Z")

</div>

Please Arup - do not pick up bad habits.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 8, 2014, 12:29pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/6 "2014-09-08T12:29:50Z")

</div>

Hello,

Since we’re on topic about underscore what is the meaning of ‘\_’ here:

> <https://github.com/rails/rails/blob/353b88135f14a928352d4cf2f5f8e4ddacbd9d4a/activerecord/lib/active_record/schema_dumper.rb#L110>

- 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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Andrew V.

Panagiotis (atmosx) Atmatzidis

email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
URL: [http://www.convalesco.org](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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 8, 2014, 2:55pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/7 "2014-09-08T14:55:33Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 8, 2014, 1:00pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/8 "2014-09-08T13:00:06Z")

</div>

On Mon, Sep 8, 2014 at 12:28 PM, Panagiotis A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> Since we’re on topic about underscore what is the meaning of ‘\_’ here:

> <https://github.com/rails/rails/blob/353b88135f14a928352d4cf2f5f8e4ddacbd9d4a/activerecord/lib/active_record/schema_dumper.rb#L110>

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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 9, 2014, 1:48am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/9 "2014-09-09T01:48:41Z")

</div>

On 8 September 2014 22:54, Alex C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.​

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 11, 2014, 11:28am UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/10 "2014-09-11T11:28:48Z")

</div>

Robert H. wrote in post #1157020:

> Please Arup - do not pick up bad habits.

What did you mean ? 🙂

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [September 8, 2014, 10:26pm UTC](https://www.ruby-forum.com/t/what-is-the-significance-of-the-leading-trailing-underscores-with-instance-variable-names/231922/11 "2014-09-08T22:26:08Z")

</div>

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
