1 / 11

Ruby Modules: An Introduction

Ruby Modules: An Introduction. Joshua Jennings. Modules. Similar to classes Cannot have instances Cannot have sub classes. Format. module ModuleName … … … … end. Why use Modules?. Superior organization Provides a namespace Alternative to multiple inheritance. Basic Example.

bond
Download Presentation

Ruby Modules: An Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby Modules: An Introduction Joshua Jennings

  2. Modules Similar to classes Cannot have instances Cannot have sub classes

  3. Format module ModuleName … … … … end

  4. Why use Modules? Superior organization Provides a namespace Alternative to multiple inheritance

  5. Basic Example module CellPhone def Call puts "This device called someone." end defVoiceMail puts "This device sent voicemail." end end

  6. Basic Example Continued require 'singleton' require 'CellPhone.rb‘ class Smartphone include Singleton include CellPhone def Info puts "This device is a smartphone." end end

  7. Multiple Inheritance When classes can have multiple parent classes Ruby does not support this However, with modules, there is an alternative

  8. How would we do this in Ruby?

  9. The Answer: Mixins While multiple inheritance is not supported, a Ruby class can include the attributes of multiple modules. The functionality of the modules are “mixed in” to the Ruby class.

  10. Basic Example Modified module PC defPlayGames puts "This device ran a video game." end def Keyboard puts "This device has a keyboard." end end

  11. Basic Example Modified require 'singleton' require 'CellPhone.rb' require 'PC.rb' class Smartphone include Singleton include CellPhone include PC def Info puts "This device is a smartphone." end end Smartphone.instance.PlayGames Smartphone.instance.Keyboard Smartphone.instance.Call Smartphone.instance.VoiceMail Smartphone.instance.Info

More Related