Using code already written and prepared by other developers within your own applications
Projects and Using Code from Other Files
Basic File Inclusion
Inclusions from Other Directories
Logic and Including Code
Nested Inclusions
Basic File Inclusion
Uses the require or load command to load the external file into the current program
#file: string_extensions.rb class String def vowels self.scan(/[aeiou]/i) end end #file: vowel_test.rb require 'string_extensions' puts "This is a test".vowels.join('-')
Basic File Inclusion (Cont.)
Ruby programmers generally use require rather than load. The effects of load are only useful if the code in the external file has changed or if it contains active code that will be executed immediately
#file: b.rb load 'a' puts "Hello from b.rb" load 'a' puts "Hello again from b.rb" #result output Hello from a.rb Hello from b.rb Hello from a.rb Hello again from b.rb #file: a.rb puts "Hello from a.rb"
Inclusions from Other Directories
The process of require and load
First looks the file in the current folder
Second looks the file in the various folders where Ruby stores its own file and libraries
Lists of directories to search for included files in a special variable called $:.
$:.each { |d| puts d } /usr/local/lib/ruby/site_ruby/1.8 /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.8.1 /usr/local/lib/ruby/site_ruby ...
Code from files that are included into others with require and load has the same freedom as if the code were pasted directly into the original file.
#file: a.rb require 'b' #And b.rb contains the following: require 'c' #And c.rb contains the following: def example puts "Hello!" end #And d.rb contains the following: require 'a' example
Libraries
Collection of routines that can be called by separate programs, but that exist independently of those programs.
A list of all the standard libraries, including documentation, is available at http://www.ruby-doc.org/stdlib/
How to use two standard libraries
net/http Library
HTTP stands for HyperText Transfer Protocol, and it’s the main protocol that makes the World Wide Web work, as it provides the mechanism by which Web pages, files, and other media can be sent between Web servers and clients
require 'net/http' Net::HTTP.get_print('www.rubyinside.com', '/') require 'net/http' url = URI.parse('http://www.rubyinside.com/') response = Net::HTTP.start(url.host, url.port) do |http| http.get(url.path) end content = response.body
OpenStruct Library
More flexible than Struct
Creates data objects without specifying the attributes
Create attributes on the fly
#struct Person = Struct.new(:name, :age) me = Person.new("Fred Bloggs", 25) me.age += 1 #openStruct require 'ostruct' person = OpenStruct.new person.name = "Fred Bloggs" person.age = 25
RubyGems
Packaging system for Ruby programs and libraries.
Manages different versions of the same libraries on your PC
Installing RubyGems
Before you can use RubyGems, it’s necessary to install it
On Windows, if you installed Ruby using the “one-click installer”, you already have RubyGem installed
On Mac OS X, Linux, and Other Unix
After you installed you can type gem
Finding Gems
Lists of the gems that are installed
gem list
Queries the remote gem server
gem list --remote gem query --remote --name-matches class
Queries the remote gem server and match the name “class”
0 comments
Post a comment