Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a macro in Rails?

I was reading the Rails docs, and I have encountered the term macros. I can't find the definition of the term (in the context of Rails). Can someone point me to a place where the term is defined?

I am familiar with the term in a different context as a software for "recording" actions (mouse, keyboard actions in OS, for instance). Is there any connection between the different uses of the term?

like image 962
blu Avatar asked Sep 18 '25 16:09

blu


2 Answers

In this context, a macro is a piece of code that generates some other code. For example:

attr_accessor :foo

generates this:

def foo
  @foo
end

def foo=(val)
  @foo = val
end

Is there any connection between the different uses of the term?

Kind of. You could say that author of attr_accessor "recorded" what it should expand to.

like image 100
Sergio Tulentsev Avatar answered Sep 20 '25 06:09

Sergio Tulentsev


In Ruby a macro is like a method, just some code, that instead of returning a Ruby datatype returns more Ruby code! This code will get executed along with all the other code you have written when you run your program.

like image 29
Aaquib Jawed Avatar answered Sep 20 '25 07:09

Aaquib Jawed