[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: attr_protected macro

Martin DeMello

3/16/2007 7:13:00 PM

On 3/16/07, gaurav bagga <gaurav.v.bagga@gmail.com> 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

3 Answers

Tim Becker

3/16/2007 8:13:00 PM

0

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

Brian Candler

3/16/2007 10:03:00 PM

0

On Sat, Mar 17, 2007 at 04:12:46AM +0900, Martin DeMello wrote:
> On 3/16/07, gaurav bagga <gaurav.v.bagga@gmail.com> 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.

Martin DeMello

3/16/2007 10:31:00 PM

0

On 3/17/07, Tim Becker <a2800276@gmail.com> wrote:
> >(of the Class class, to be precise)
> <smartass>of the class Module, to be precise</smartass>

I had a feeling I should've looked it up first :)

martin