tim_xyz tim_xyz - 1 year ago 60
Ruby Question

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

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.

Answer Source

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:


If you want to know what this inherits from:

@a_form.superclass # Immediate parent
@a_form.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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download