an intro to modules clint conwell cpsc 5135. ruby and modules modules in ruby are very similar to...

10
An Intro To Modules Clint Conwell CPSC 5135

Upload: charles-terry

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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:

An Intro To Modules

Clint ConwellCPSC 5135

Page 2: 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:

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.

Page 3: 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:

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!

Page 4: 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:

EXAMPLE• Let’s describe the characteristics of N.F.L. teams with a module!

• Consider the following module:module ProTeamnumber_of_players = 53number_of_ starters = 22class Quarterbackdef positionputs ‘Behind center…’enddef roleputs ‘Runs offense, throws football, runs football…’endendClass Centerdef positionputs ‘Middle of Offensive Line’enddef roleputs ‘Snaps ball, blocks!’endend..#So on and so on for each position..end

Page 5: 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:

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!

Page 6: 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:

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

Page 7: 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:

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.

Page 8: 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:

Mixin it Up (cont’d)

• Class Packersinclude ProTeaminclude Stadiuminclude Coach# and so on

endClass Saints

include ProTeaminclude Stadiuminclude Coach#and so on

end

Page 9: 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:

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?

Page 10: 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:

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.