Selective Inspect
Simple gem to customize the output of the #inspect
method.
Ruby's default #inspect
implementation prints EVERY instance variable in your object objects, which can be really painful sometimes.
This gem allows to define a whitelist of the instance variables you want to output or a blacklist of those you don't.
Installation
Add this line to your application's Gemfile:
gem 'selective_inspect'
And then execute:
$ bundle
Or install it yourself as:
$ gem install selective_inspect
Usage
class Game
end
game = Game.new(difficulty: 'hard', mode: 'deathmatch')
game.inspect
SelectiveInspect.perform_inspect(game, :mode)
class Player
include SelectiveInspect
inspectable_vars :id, :nickname, :score, :health, :ip_address
end
class Weapon
include SelectiveInspect
uninspectable_vars :range, :damage
end
weapon = Weapon.new(type: 'Bazooka', ammo: 3, range: 600, damage: 'max')
player = Player.new(id: 1, name: 'John', health: 100, weapon: weapon)
player.inspect
player.inspect(:nickname, :ip_address)
TODOs (by priority)
- Allow to pass a list of instance methods to inspect along with the instance variables
- Avoid infinite recursion checking for cycles.
- Rails environment integration: Only enable it for development and test.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request