Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active July 22, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/7d18c3758b04f726ef9b9277e32b3516 to your computer and use it in GitHub Desktop.
Save JoshCheek/7d18c3758b04f726ef9b9277e32b3516 to your computer and use it in GitHub Desktop.
Wat is a PORO?
# There is no official definition, this is the one I propose:
# Something is a PORO if it inherits from Object.
# The further you stray from that, the murkier it gets.
# Note that "A inherits from B" is basically the same thing as "A's superclass is B"
# Explicitly inheriting from Object
class Poro1 < Object
end
Poro1.superclass # => Object
# Implicitly inheriting from Object
class Poro2
end
Poro2.superclass # => Object
# The reason people made up the word PORO was because
# if you come into Rails or something, you know about
# Models and Views and Controllers. If you want a noun
# in that world, you might go make a database table,
# because that’s the only way you know of to get an object
# with some attributes. But for a lot of things, you don’t
# need any of that stuff, you don’t need to start a database
# or a server or make an HTTP connection, you just want an
# object with a method you can call, a plain old ruby object
# will do just fine, hence PORO. It gives a name to the idea
# that something can be a simple straight-forward standard
# every-day run-of-the-mill plain-old ruby object without
# any cray cray magic behind it.
# Here is an example of something that is NOT a poro,
# I literally have to make a database to create this class!
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table(:not_poro) { |t| t.timestamps }
end
class NotPoro < ActiveRecord::Base
end
# The PORO inherits from Object, the not-PORO inherits from not Object
Poro1.superclass # => Object
NotPoro.superclass # => ActiveRecord::Base
# The PORO has a standard set of methods (inspect, ==, clone, ...)
# The not-PORO has lots of inherited methods that you don't know how they work or what they do
Poro1.instance_methods.length # => 83
NotPoro.instance_methods.length # => 305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment