Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

6 Modules Inheritance Gems

1,280 views

Published on

Published in: Technology
  • Be the first to comment

6 Modules Inheritance Gems

  1. 1. Class 6<br />Modules, Inheritance & Building a Gem<br />
  2. 2. I am a module!<br />module Awesome<br /> #a bunch of method definitions that give<br /> #really cool functionality<br />end<br />class Rad<br /> include Awesome<br /> #more code<br />end<br />Now Rad class can call all the classes and methods defined in Awesome as though they were in Rad<br />
  3. 3. Modules... <br />Help with program design and flexibility<br />Encourage modularity<br />
  4. 4. No Multiple Inheritance in Ruby<br />Without modules, a class can only inherit from one source, its superclass. <br />You can “mix in” as many Modules as you like. <br />A module can be mixed in to multiple classes.<br />
  5. 5. Add the same functionality to multiple classes<br />module Enumerable<br /> def each<br /> #lots of code<br /> end<br /> #lots more code<br />end<br />class Array<br /> include Enumerable<br />end<br />class Hash<br /> include Enumerable<br />end<br />
  6. 6. Method Search Path<br />dog= Dog.new<br />dog.talk<br />class BasicObject<br />class Object<br />modules mixed in to Object class<br />class Animal<br />modules mixed in to Animal class<br />class Dog<br />modules mixed in to Dog class<br />
  7. 7. Building a Gem<br />write some Ruby code that you want to make available as a gem<br />creating a gem specification file (mygem.gemspec), which is Ruby code<br />running gem build mygem.gemspecto create the gem file (mygem-2.1.gem)<br />More info: http://docs.rubygems.org/read/chapter/5<br />
  8. 8. Gem Specification<br />spec = Gem::Specification.new do |s| s.name = &apos;rfoo&apos; <br />s.version = &apos;1.0&apos; <br />s.summary = &apos;example gem&apos; <br />s.files = [&apos;myfile.rb&apos;, &apos;README&apos;, &apos;LICENSE.txt&apos;] <br />end<br />By convention, source goes into a /lib directory<br />Then you would put your specs into spec/lib<br />More info: http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html<br />
  9. 9. Building & Installing the Gem<br />gem build test_data.gemspec<br />--&gt; now you have a gem file: test_data-0.0.1.gem<br />gem list test_data<br />--&gt; but you don&apos;t have access to the gem yet, you need to install it<br />gem install test_data-0.0.1.gem <br />gem list test_data<br />
  10. 10. Using the Gem We Built<br />$ irb<br />&gt;&gt; load &apos;test_data&apos;<br />LoadError: no such file to load -- test_data<br />from (irb):1:in `load&apos;<br />from (irb):1<br />&gt;&gt; require &apos;test_data&apos;<br />=&gt; true<br />&gt;&gt; TestData::create_names(&apos;foo&apos;, 10)<br />=&gt; nil<br />
  11. 11. Gem Resources<br />Rdoc<br />http://rdoc.sourceforge.net/doc/index.html<br />gem server<br />http://localhost:8808<br />publish your gems <br />http://gemcutter.org/<br />
  12. 12. Time to Party.<br />Thank You!<br />

×