1 / 10

An Intro To Modules

An Intro To Modules. Clint Conwell CPSC 5135. Ruby and Modules. Modules in Ruby are very similar to classes The syntax for defining a module is simply: module name_of_module . . end However, there is a caveat, a module can not have an instance…this will be illustrated later. Purpose?.

sulwyn
Download Presentation

An Intro To Modules

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. An Intro To Modules Clint Conwell CPSC 5135

  2. Ruby and Modules • Modules in Ruby are very similar to classes • The syntax for defining a module is simply: module name_of_module . . end • However, there is a caveat, a module can not have an instance…this will be illustrated later.

  3. Purpose? • So, if Modules are like classes, yet can not be instantiated…why? • One reason is to have a consolidation of related methods and constants. • Another is to implement what is known as a mixin! The term mixin refers to a technique where we include a module in a class, and the class “inherits” the module’s methods and constants. This is what makes modules really useful. • See next slide for example and code!

  4. EXAMPLE • Let’s describe the characteristics of N.F.L. teams with a module! • Consider the following module: module ProTeam number_of_players = 53 number_of_ starters = 22 class Quarterback def position puts ‘Behind center…’ end def role puts ‘Runs offense, throws football, runs football…’ end end Class Center def position puts ‘Middle of Offensive Line’ end def role puts ‘Snaps ball, blocks!’ end end . . #So on and so on for each position . . end

  5. So, now what? • We can access classes, methods, and constants from the module by using the :: notation as follows: Peyton_Manning = ProTeam::Quarterback.new on_the_bench = ProTeam:: number_of_players - ProTeam::number_of_starters • Big deal right? Don’t blow off modules yet. The real usefulness of modules is the mixin technique! • Move on to the next slide!

  6. Mixin it Up! • Suppose we wanted to create a class that represents a football team… • You can include the ProTeam module! • Class Redskins include ProTeam … end

  7. Mixin it Up (cont’d) To illustrate the convenience of modules, think about the characteristics of an NFL team, or any pro sports team. Every team has a stadium or an arena. Every team has a coaching staff. Every team has non- personnel. We can create a module to represent each of these characteristics that constitute a professional sports team, and use them to build multiple teams.

  8. Mixin it Up (cont’d) • Class Packers include ProTeam include Stadium include Coach # and so on end Class Saints include ProTeam include Stadium include Coach #and so on end

  9. Mixin it Up (cont’d) • So now we can easily create classes that represent NFL teams and their basic characteristics. Now all we have to do is code the unique characteristics of these teams. Sure beats reinventing the wheel every time, huh?

  10. Modules, In Conclusion • This was a brief introduction to modules. It is easy to see how to implement modules and how they can assist a Ruby programmer. • To recap, Modules have two particularly powerful uses, consolidation of like methods and constants, and to be utilized as a mixin with Ruby classes.

More Related