Metaprogramming: #const_set

You’re closer than you think to a career in tech. Our grads have launched rewarding new careers — you can too.

View Our Jobs Report

The following is a guest post by Kevin Curtin and originally appeared on his blog. Kevin is currently a student a The Flatiron School. You can follow him on twitter here.

This code sample is from the Proto gem

The Goal

To dynamically create classes based on the parameters of a method

The Code

Blog post image: tumblr_mew234Bzgo1rtan47.png

The Breakdown

We are used to creating classes like this:

Blog post image: tumblr_mew23bwW8H1rtan47.png

Since everything is an object in ruby, it turns out that we can create a class as we would any other object: new_class = This instantiates a new class that inherits from whatever you pass in as the parameter. In this case, OpenStruct.

One of the requirements for creating the return objects for Proto is that they are namespaced. We don’t want the constants we return to conflict with names that already exist in the code base. Modules have a #const_set method that allows you define a new constant within the module. It accepts two arguments, the constant name and the value. Since we are creating a new class, we’ll pass in the name of our constant that was passed in with the initial method call and then pass in the class we just created:

Blog post image: tumblr_mew23yo3M41rtan47.png

If, for example, the user has passed in ‘Tweet’ to the .create_return_objectsmethod, they will get back Proto::Tweet objects.

We can now access this new class using the #const_get method, newing up the class and passing in our attributes. Since the class we created inherits from OpenStruct and is just a value object, we don’t ever have to open it up and define behavior. Proto.const_get(name).new(hash)


Headshot of Flatiron School

Flatiron School

Blog Post Author

Read More Flatiron School Articles

Since we opened our doors in 2012, thousands of students have joined Flatiron School to launch new careers in tech.

Explore our Courses

Find the perfect course for you across our in-person and online programs designed to power your career change.

Explore Our Courses
Join a Community

Connect with students and staff at meetups, lectures, and demos – on campus and online.

Join the Community
Schedule a Chat

Have a question about our programs? Our admissions team is here to help.

Schedule a Chat