Macro Magic
Coming from a career where Excel was used frequently, I had the chance to experiment with macros. These macros would allow me to automate tasks like formatting a spreadsheet, saving time and requiring less action on my part. When I began learning Ruby, I was re-introduced to the idea of macros for programming.
In programming, a macro is a piece of code that runs another code block. Think of a function (JavaScript) or method (Ruby), but instead of returning data, we get back more code. This is incredibly useful as it can help us as developers writer cleaner code in less lines. Macros also help us follow the “DRY” principle, or “Don’t Repeat Yourself”. Let’s take a look at what I mean.
Get Matthew Draghi’s stories in your inbox
Join Medium for free to get updates from this writer.
In Ruby and other object-oriented languages, two common methods that need to be created are the “setter” and “getter” methods. They would like like this:
class Movie #setter method
def title=(title)
@title = title
end #getter method
def title
@title
endend
The example above is in Ruby. In this sample, the setter method is taking an argument of “title” and setting that value as the value of our instance variable “@title”. The getter method below the setter method just takes the instance variable of “@title” and returns, or gets, the value. We use these methods like so:
#first we create a new instance of the Movie object
movie = Movie.new#now we can use the title= method to set the value of our movie's title movie.title=("Pulp Fiction")#with our title set, we can use our getter to retrieve the value of our title movie.title #=> "Pulp Fiction"
The setter and getter methods are simple methods, but when our objects start requiring more attributes, it can quickly take up a large portion of our class file…
class Movie#setter method
def title=(title)
@title = title
end#getter method
def title
@title
end def director=(director)
@director = director
end def director
@director
end def stars=(stars)
@stars = stars
end def stars
@stars
end ...end
…you get the point. Even by just adding two more attributes, we see the file can become pretty hefty. Lucky for us, Ruby has some built in macros that help us clean this up a bit more. The attr_accessor, attr_reader, and attr_writer macros are just a few macros that Ruby has to offer which help us create the setter and getter methods. Let’s take a look at our Movie class when we use these macros.
class Movie attr_accessor :title, :director, :starsend
This is a lot cleaner and succinct than what we did above. The attr_accessor macro does the work of creating a setter and getter for us. The other macros I mentioned work in a similar way — the attr_writer method creates the setter and the attr_reader creates the getter. What we are telling Ruby to do here is create the setter and getter methods for the different attributes listed (title, director, and stars). This allows us to call the methods on our instances as we normally would, but without the hassle and mess of creating the methods in our class file.
As we can see, macros can make a developer’s experience on a project a much more enjoyable one. Setters and getters aren’t the only application for macros. We can see through gems like Active Record that macros can be used for other things like SQL queries. Once we start getting into complex databases, the belongs_to and has_many macros are just two ways to make pulling specific data from related tables based on foreign keys easier. Being as powerful as they are, be sure to take time to experiment with the common macros. Getting an understanding of how they work can help you write more clean code that is DRY.







