# Can someone explain this to me

**URL:** <https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733>\
**Category:** Rails\
**Created:** [February 21, 2008, 4:23am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733 "2008-02-21T04:23:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 4:23am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/1 "2008-02-21T04:23:31Z")

</div>

Hi

I was reading some articles and I came across this one in how to manage  
the database.yml file when you have multiple developers. (To prevent  
passwords and such being in SVN etc…)

```auto
login: &login
  username: defaultuser
  password: defaultpassword

<%= file = File.join(RAILS_ROOT, "config", "dblogin.yml")
    IO.read(file) if File.exist?(file) %>

development:
  adapter: mysql
  host: localhost
  database: foo_development
  <<: *login
```

Firstly where I’m confused is the &login block (near login:) How is this  
ever processes or passed info and subsequently where is it referenced?

Secondly the \<\<: notation. (I can’t find anything explaining this in the  
pickaxe book or online), but I bet I"m just missing it. What’s it  
purpose?

The “ERB” part where it reads a file if it exists and adds the info in  
this file TO the database.yml is pretty clear but why the login block  
and the \<\<: ?

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 5:53am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/2 "2008-02-21T05:53:51Z")

</div>

cd config  
svn propset svn:ignore database.yml .

This will ignore the database.yml file providing you haven’t yet  
committed  
it. If you have committed it, delete it and then do that line.

On Thu, Feb 21, 2008 at 1:53 PM, Jean N. \<  
removed\_email\_address@domain.invalid\> wrote:

> password: defaultpassword
> 
> ## Thanks.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 6:18am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/3 "2008-02-21T06:18:28Z")

</div>

> IO.read(file) if File.exist?(file) %\>
> 
> Secondly the \<\<: notation. (I can’t find anything explaining this in the  
> pickaxe book or online), but I bet I"m just missing it. What’s it  
> purpose?

The “\<\<:” line along with the “login: &login” line is saying that the  
entries under the login: block (cause it’s associated with &login, not  
cause the first part is login) should be included in the development  
section.

Basically it let’s you share common entries across all your entries  
without having to specify them over and over again.

But like Ryan said, don’t track database.yml in SVN. Ignore it. Create  
a  
database.yml-production that gets moved into place for production and  
let  
each developer create their own database.yml for their environment.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 11:23am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/4 "2008-02-21T11:23:51Z")

</div>

On 21 Feb 2008, at 05:49, Jean N. wrote:

> the username: password: From the read file and not all the other  
> stuff.

Because yaml uses indentation to track how far a chunk goes (or in  
other words, the same way yaml knows that

foo:  
id: 1  
bar:  
id: 2

in one of your fixtures files represents 2 rows in the database, not one

Fred

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 6:49am UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/5 "2008-02-21T06:49:38Z")

</div>

> The “\<\<:” line along with the “login: &login” line is saying that the  
> entries under the login: block (cause it’s associated with &login, not  
> cause the first part is login) should be included in the development  
> section.
> 
> Basically it let’s you share common entries across all your entries  
> without having to specify them over and over again.
> 
> But like Ryan said, don’t track database.yml in SVN. Ignore it. Create  
> a  
> database.yml-production that gets moved into place for production and  
> let  
> each developer create their own database.yml for their environment.

(Thanks to you and Ryan both on your help)

Maybe I should ask if there is a reference somewhere I can read up on  
this…

but I’m still confused how the block is begun/ended. It’s easy enough to  
see with do/end or curly braces…but this notation is just foreign to  
me. (Summarized question, how does the \<\<: \*login know to insert only  
the username: password: From the read file and not all the other stuff.

Sorry I realise I’m moving off Rails into more Ruby questions now…

Your help is very appreciated.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/6 "2008-02-21T16:14:12Z")

</div>

On Feb 21, 2008, at 12:49 AM, Jean N. wrote:

> > Create  
> > but I’m still confused how the block is begun/ended. It’s easy  
> > enough to  
> > see with do/end or curly braces…but this notation is just foreign to  
> > me. (Summarized question, how does the \<\<: \*login know to insert only  
> > the username: password: From the read file and not all the other  
> > stuff.
> 
> Sorry I realise I’m moving off Rails into more Ruby questions now…
> 
> Your help is very appreciated.

The issue is that &login is not a “block”, this is YAML not Ruby. You  
read up on it go to the spec linked from [YAML&nbsp;Ain’t&nbsp;Markup&nbsp;Language&nbsp;(YAML™) Version&nbsp;1.1](http://www.yaml.org/spec/1.1/)  
the YAML home page.

As for whether to keep database.yml in the repository, I vote yes –  
even if you put a different file in place on production during  
deployment. Here’s how I’ve done it:

# vvv Copy the following two YAML maps to a file called config/

mydatabase.yml

login: &login  
username: default\_user  
password: default\_password

connection: &connection  
host: 127.0.0.1  
port: 3306  
encoding: utf8

# ^^^ Copy the previous two YAML maps to a file called config/

mydatabase.yml

\<%= file = File.join(RAILS\_ROOT, “config”, “mydatabase.yml”)  
IO.read(file) if File.exist?(file) %\>

development:  
adapter: mysql  
database: projectname\_development  
\<\<: \*login  
\<\<: \*connection

Those lines are taken directly from a database.yml (changing only  
username, password, and database to protect the guilty). This file is  
in the repository. I split the login credentials from the connection  
parameters to deal with the port versus Unix socket of MySQL and on a  
recent project, even split out the adapter so cope with PostgreSQL  
versus MySQL.

The mydatabase.yml file contains just those YAML maps that need to be  
changed from the default. Typically I only have the connection  
specified. The mydatabase.yml file is never in the repository. I  
keep mydatabase-production.yml locally as a backup to the one in  
production. I’ve also seen a similar arrangement that reads the  
separate YAML file a second time at the end of the database.yml, but I  
don’t recall the reason that was done (although I remember that the  
explanation made sense for what was being done).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/7 "2008-02-21T16:17:32Z")

</div>

Jean N. wrote:

> Then if I put (anywhere in the yaml file) \<\<: &a\_block it will inline  
> everything I have defined as that block?

This should have read “\<\<: \*a\_block it will inline…”

Sorry.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [February 21, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-to-me/129733/8 "2008-02-21T16:16:24Z")

</div>

Frederick C. wrote:

> On 21 Feb 2008, at 05:49, Jean N. wrote:
> 
> > the username: password: From the read file and not all the other  
> > stuff.
> 
> Because yaml uses indentation to track how far a chunk goes (or in  
> other words, the same way yaml knows that
> 
> foo:  
> id: 1  
> bar:  
> id: 2
> 
> in one of your fixtures files represents 2 rows in the database, not one
> 
> Fred

Okay so

Login: &a\_block  
username: foo  
password: bar

KNOWS That I am ‘naming’ the Login section “a\_block”.

Then if I put (anywhere in the yaml file) \<\<: &a\_block it will inline  
everything I have defined as that block?

Very neat…

Am I correct in assuming \<\<: Is a yaml operator? (As I can’t find it in  
any of my references)

Thanks everyone.
