concept display in category ruby

appears as: display
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

A newly created BasicObject instance has only 8 instance methods—whereas a new instance of Object has 58. (These numbers may change a little among different versions or releases of Ruby, but they’re accurate enough to make the point about BasicObject having few methods.) You’re not likely to need to instantiate or subclass BasicObject on a regular basis, if ever. It’s mainly handy for situations where you’re modeling objects closely to some particular domain, almost to the point of writing a kind of Ruby dialect, and you don’t want any false positives when you send messages to those objects. The 58 methods can get in the way, if you have your own ideas about whether your objects should play dumb when you send them messages like display, extend, or clone. (There’ll be more to say about this when we take up the topic thread of BasicObject in chapter 13.)

Another, less frequently used, method generates and displays a string representation of an object: display.

Using display

You won’t see display much. It occurs only once, at last count, in all the Ruby program files in the entire standard library. (inspect occurs 160 times.) It’s a specialized output method.

display takes an argument: a writable output stream, in the form of a Ruby I/O object. By default, it uses STDOUT, the standard output stream:

>> "Hello".display
Hello=> nil

Note that display, unlike puts but like print, doesn’t automatically insert a newline character. That’s why => nil is run together on one line with the output.

You can redirect the output of display by providing, for example, an open file handle as an argument: