The Book of Ruby by Huw Collingbourne

The Book of Ruby by Huw Collingbourne

Author:Huw Collingbourne
Language: eng
Format: mobi, epub, pdf
Tags: COMPUTERS / Programming Languages / Ruby
Publisher: No Starch Press
Published: 2011-06-30T04:00:00+00:00


You can call a module’s module methods just as you would call a class’s class methods, using dot notation, like this:

MyModule.lose #=> "Sorry, you didn't win"

But how do you call an instance method? Neither of the following attempts succeeds:

puts( prize ) # Error: undefined local variable or method

puts( MyModule.prize ) # Error: undefined method 'prize'

In spite of their similarities, classes possess two major features that modules do not: instances and inheritance. Classes can have instances (objects created from the class), superclasses (parents), and subclasses (children); modules can have none of these. It is not possible to call an instance method from an instance of a module (a “module object”) for the simple reason that it is impossible to create instances of a module. This explains the errors when you try to call the prize method in the previous code.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.