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