How to decorate any object in Ruby in 3 lines

twitter logo github logo ・1 min read

Here is a quick and easy way to decorate any object instance in Ruby.

This is useful if you don't know or care the object type you are working with but would like to add accessor methods to just that instance. This is a very pure decorator because adding accessors does not rely on internal knowledge of the object.

We're just dynamically adding functionality to the instance as we need it.

NOTE: This could clobber existing functionality, so you want be sure you are decorating it with a method name that won't interfere with the object class.

twitter logo DISCUSS
Classic DEV Post from May 6 '19

The state of GraphQL

The report about the state of GraphQL base on the partial data gathered during the annual "The State of JavaScript" survey

Peter P profile image
Co-founder/CTO of Recognize, Managing Partner of Planet.io Author of The Ruby Workshop

Still haven't created a profile?

DEV is open source, committed to the open web, and quickly becoming the biggest world's biggest network of software developers.

Get started now ❤️