Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if an object is a PORO or not?

Tags:

oop

ruby

When I look at an object (in the terminal or rails console), how can I determine if it is PORO or not?

I understand it means an object is just Ruby (does not rely on Rails). But I don't understand in a practical sense.

For example, when I print puts @a_form.inspect to the console, is it PORO?

#<AForm:0x007fdbf9b33468 @a=#<A id: 1,...

I've done lots of googling but haven't found a simpler answer with examples comparing to non-PORO objects.

Can anyone give a practical comparison?

Apologies in advance if this is a super newbie question.

like image 830
tim_xyz Avatar asked Aug 24 '16 16:08

tim_xyz


2 Answers

The distinction between a PORO (which stands for Plain Old Ruby Object) and a not-PORO is not a technical one. All objects are technically Ruby objects.

Instead, the term PORO is sometimes used when talking about large frameworks like Rails which tend to use "big", "complex" objects like ActiveRecord models for much of its logic. These large objects tend to contain a large amount of logic and (often implicitly) defined behavior and conventions.

When talking about POROs, it's generally done to differentiate from these large objects and to encourage instead to use smaller, specific classes/objects to build your business logic which may look more simple. These objects often don't need to be backed by stable storage as ActiveRecord models generally are but just contain in-flight data and define business logic (what you generally call the behavior of your application) without using much tooling from a complex framework.

But again, this is not a strict technical distinction or even one with clear boundaries. It is a social distinction one which generally aims to encourage thinking outside of the single framework.

As for the term itself, it is relatively new and (you probably guessed it by now) pretty devoid of any actual hard meaning. It got popular recently with people complaining about the structure some large Rails apps tend to have nowadays (such as lean controller, fat model where you are putting the whole business logic into ActiveRecord models. The term thus mostly just serves as a distinction from these fat models.

like image 192
Holger Just Avatar answered Nov 13 '22 12:11

Holger Just


There's really no such thing as a "plain old Ruby object" as everything, literally everything is an object anyway, so there's no concept of "plain". How would you define non-plain?

This is unlike JavaScript where there are simple object primitives and then others that are more formally declared using things like the ES6 class construct. A JavaScript object being passed around as a container is a common pattern. The equivalent in Ruby would be passing a complex data structure like a Hash with potentially nested elements.

If you want to know the class of an object:

@a_form.class

If you want to know what this inherits from:

@a_form.superclass # Immediate parent
@a_form.class.ancestors # All base classes and mixin modules

There are anonymous classes in Ruby, those made with Class.new, and objects created with those are about as plain as you can get, but they're still first-class objects.

like image 43
tadman Avatar answered Nov 13 '22 13:11

tadman