# What this line means exactly? (@@active\_connections ||= {})

**URL:** <https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828>\
**Category:** Rails\
**Created:** [January 24, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828 "2006-01-24T21:35:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828/1 "2006-01-24T21:35:00Z")

</div>

Hi,

I have problem to understand this line does exactly?  
this is from the class ConnectionSpecficication,  
methode “active\_connections”

def self.active\_connections #:nodoc:  
if allow\_concurrency  
Thread.current[‘active\_connections’] ||= {}  
else  
@@active\_connections ||= {}  
end  
end

what the line “@@active\_connections ||= {}” means  
exactly?  
as I undertands is if the class varaible  
@@active\_connections has a value then return the  
value, otherwise return {}, but what is {}, a bloc of  
nothing??? or what?

Thanks you very much

Saiho

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828/2 "2006-01-24T21:41:05Z")

</div>

On 1/24/06, Saiho Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @@active_connections ||= {}
> 
> ```
> 
> end  
> end
> 
> what the line “@@active\_connections ||= {}” means  
> exactly?  
> as I undertands is if the class varaible  
> @@active\_connections has a value then return the  
> value, otherwise return {}, but what is {}, a bloc of  
> nothing??? or what?

a = 2  
a ||= 3

# a is 2

b ||= 3

# b is 3

So, ||= will set a variable to some value if the variable doesn’t have  
a value yet.

{} is a hash. Could’ve used:  
@@active\_connections ||= Hash.new  
instead.

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828/3 "2006-01-24T21:41:05Z")

</div>

{} is an anonymous hash

It seems to be saying

“return whatever it is, or initialize it as an empty hash and return  
it.”

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 25, 2006, 1:14am UTC](https://www.ruby-forum.com/t/what-this-line-means-exactly-active-connections/96828/4 "2006-01-25T01:14:02Z")

</div>

Yeah, it will use @@active\_connections if it has been set, otherwise it  
will set @@active\_connections to an empty hash and return it.

One thing about this feature has caught me out a couple of times. Look  
at this:

x = false  
x ||= 4

# x = 4 !

x is set to 4 even though it has already been set to false. It does this  
because ||= is simply a shortcut for x = x || 4, and (false || 4)  
results in 4.

Interesting 🙂

-Jonny.

Kelly Dwight F. wrote:

> {} is an anonymous hash
> 
> It seems to be saying
> 
> “return whatever it is, or initialize it as an empty hash and return  
> it.”
