Creating dynamic attribute accessors
Tags: webdev
Published: 2019-04-26 14:30:49

---
title: Creating dynamic attribute accessors
published: true
description: 
tags: webdev,meta, 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/g8irzuqcucooomihazqe.PNG
canonical_url: https://www.2n.pl/blog/dynamic-attribute-accessors
---

Metaprogramming, the art of messing with the internals of a programming language, is one of the most powerful learning tools an aspiring programmer can utilize. It's very easy to take the workings of the given language as they are for granted, but there's hardly any room for improvement in doing so. Quite the contrary, being oblivious to how the things work behind the scenes may make you unable to grasp many of the concepts that you may use in the project on daily baisis. Unfortunately for metaprogramming, as the most the projects don't really delve into this arcane realm, the opportunity to put it into use may not come for a long time, if ever.

This is not good. Let's warp the reality and create our own opportunities. What else could you possibly do in your free time anyway? Play video games? What a nerd.

For the higher purpose of expanding our knowledge, we must forget (just a little) about practical usage of the fore coming examples. For now, only our goal matters and the means of how we achieve it. Let's say, that for some weird, possibly sexual reason you'd want to create an initializer for your class, that would take any number of hashed arguments and create corresponding accessors for each of them. Why would you want to do that? I don't know nor care, but we sure as hell are going to do that.

So we want this:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epubjg3x4uypfxga09q5.png)

to be as viable as this:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohjxhrkv32lkb1b54u67.png)

And to be able to access values of these variables like this:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n949m1op5xy873h498ts.png)

And set them like this:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwyny5avjidpndwgjdbr.png)

So, to unpack it all: we need to make our initialize method accept a hash of arguments, iterate through them, set them as instance variables and create an accessor for each of them. Let's try this:
![](https://thepracticaldev.s3.amazonaws.com/i/mycmxhp7mxdwhy8uflu1.png)

Looks good. Double splat operator (**) is an indicator of arguments being processed as a hash. 'Instance variable set' is our go-to way of...you know, it's pretty self-explanatory, so I'll just let you figure that out yourself. The last line is the call to our class' private method with an arguments being names of the variables that we previously set as the instance variables and which we want to be able to access or set. OK let's find out what the fallout is:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gk8q5urljgwdngby3u5h.png)

Ok, seems to be working well...if you are willing to disregard this:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swp59ucywytzgmnv2lsr.png)

You see, our 'send' method have been acting directly on our class, so the accessors we set like this aren't going nowhere and are freely available to all future instances of this class. This is a mess and we can't have that. Accessors we set should be constricted only to the arguments which we initialize our class instances with and available only for given instance. So what we really need to do, is to kinda cheat our way into the accessor-like behaviour by creating per-instance methods which simply return out previously set instance variables or set new values for them. Here comes the question: did you know that singleton methods can be created for instances as well? I know what you'll think: it's almost ultra rare in the environment of run-of-the-mill projects. Most certainly, but why bother with boring reality, when we can create our own occurrences? Let's do this right now:
![](https://thepracticaldev.s3.amazonaws.com/i/s8igv8p3vz83he8pyiet.png)

This time we completely make do without 'attr_accessor' business. Instead, iterating through our arguments we create two singleton methods per argument - one of them simply named as your variable name which simply call the inverse of previously used 'instance_variable_set' - 'instance_variable_get' to get its' value, second - with equal sign following the variable name (and also acccepting additional argument) for setting new value to our variables. Let's find out where the whole affair has gotten us:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lc71lku8u091x2yjv60.png)

Voila. No access to the first instance's variable from the second one, getters and setters working as intended. Now, as far as metaprogramming goes, this is a pretty basic and primitive example, but it's only meant to show that generally, programming languages hold many neat tricks, that can enable you to bend the rules of the game to your will.