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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With