Attr_protected macro

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?

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 (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:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001005

-Tim

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

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

On 3/16/07, gaurav bagga [email protected] 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

On 3/17/07, Tim B. [email protected] 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 :slight_smile:

martin

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

On 3/16/07, gaurav bagga [email protected] 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.