# Attr\_protected macro

**URL:** <https://www.ruby-forum.com/t/attr-protected-macro/88987>\
**Category:** Ruby\
**Created:** [March 16, 2007, 9:35am UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987 "2007-03-16T09:35:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 9:35am UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/1 "2007-03-16T09:35:38Z")

</div>

Hi All,

I was going through a blog where I found “Do this by calling the  
attr\_protected macro in your model class definition” what do you mean  
by macro here.  
Is it a method?

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 11:02am UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/2 "2007-03-16T11:02:52Z")

</div>

> I was going through a blog where I found “Do this by calling the  
> attr\_protected macro in your model class definition” what do you mean  
> by macro here.  
> Is it a method?

Ruby comes with the `attr_accessor` family of functions built in to  
`Module` ([class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html)). These  
methods allow you to specify which instance variables of a class you’d  
like accessor methods you’d like getters and setters generated for  
automatically. E.g.:

class Test  
attr\_accessor :test\_variable  
end

The above code will add roughly the following to the `Test` class:

def test\_variable  
@test\_variable  
end

def test\_variable= a  
@test\_variable=a  
end

The ‘attr\_…’ methods are regular methods, but people might call them  
“macros” because they’re basically shorthand for generating other code  
automatically. They’re not macros in the C sense of the word.

I assume this is about rails, `attr_protected` refers to:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001005)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

-Tim

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 11:07am UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/3 "2007-03-16T11:07:53Z")

</div>

> The ‘attr\_…’ methods are regular methods, but people might call them  
> “macros” because they’re basically shorthand for generating other code  
> automatically. They’re not macros in the C sense of the word.

Thanks Tim,  
I got confused by the usage of word “macro” :).

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/4 "2007-03-16T21:14:24Z")

</div>

> (of the Class class, to be precise)  
> of the class Module, to be precise

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/5 "2007-03-16T20:14:15Z")

</div>

On 3/16/07, gaurav bagga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> I was going through a blog where I found “Do this by calling the  
> attr\_protected macro in your model class definition” what do you mean  
> by macro here.  
> Is it a method?

First a bit of background: ruby has no real ‘compile’ phase; it  
executes code within a source file as it reads it. Code within a class  
Foo/end block is executed in the scope of the class Foo. Thus def  
method…end is actual code that defines a method and adds it to the  
class, etc.

Some methods, loosely called ‘macros’, define other methods on the fly  
when they’re executed, or do other things commonly lumped under  
“metaprogramming”. For instance, in the following code

class Foo  
attr\_accessor :bar  
end

attr\_accessor is a method (of the Class class, to be precise), to  
which the argument :bar is passed. When executed, it generates the  
following two methods

def bar  
@bar  
end

def bar=(x)  
@bar = x  
end

Not sure exactly what attr\_protected does, but it’s somewhere along  
the same lines.

martin

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/6 "2007-03-16T23:31:58Z")

</div>

On 3/17/07, Tim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > (of the Class class, to be precise)  
> > of the class Module, to be precise

I had a feeling I should’ve looked it up first 🙂

martin

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/attr-protected-macro/88987/7 "2007-03-16T23:03:54Z")

</div>

On Sat, Mar 17, 2007 at 04:12:46AM +0900, Martin DeMello wrote:

> On 3/16/07, gaurav bagga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi All,
> > 
> > I was going through a blog where I found “Do this by calling the  
> > attr\_protected macro in your model class definition” what do you mean  
> > by macro here.  
> > Is it a method?
> 
> First a bit of background: ruby has no real ‘compile’ phase; it  
> executes code within a source file as it reads it.

Almost. It actually reads in and _parses_ the whole file, start to end,  
and  
builds an internal representation of it: the syntax tree. _Then_ it runs  
it.

The parse may fail if your program is has invalid structure (e.g. you  
don’t  
have the right number of matching “end” statements), in which case  
nothing  
is executed.

The parse phase is also significant in that this is the point at which a  
decision is made as to whether a bare word is a local variable access,  
or a  
method call.

As an example:

if false  
x = 3  
end

def x  
“hello”  
end

puts x # nil

The line “x = 3” is never executed. However, any bare “x” from that  
point  
until the end of the scope is then treated as a local variable access,  
rather than a method call.

However the parse phase doesn’t define classes or methods; this doesn’t  
happen until the class or method definitions are _executed_.

Regards,

Brian.
