Upload
vorleak-chy
View
499
Download
1
Embed Size (px)
Citation preview
Ruby Projects Ruby Projects and and
LibrariesLibraries
Vorleak Chy ([email protected])Vorleak Chy ([email protected])
Software EngineerSoftware Engineer
Yoolk IncYoolk Inchttp://groups.google.com/group/khmertech/http://groups.google.com/group/khmertech/
AgendaAgenda
Projects and Using Code from Projects and Using Code from Other FilesOther Files
LibrariesLibraries
RubyGemsRubyGems
IntroductionIntroduction
Using code already written and Using code already written and prepared by other developers prepared by other developers within your own applicationswithin your own applications
Projects and Using Projects and Using Code from Other FilesCode from Other Files
Basic File InclusionBasic File Inclusion
Inclusions from Other DirectoriesInclusions from Other Directories
Logic and Including CodeLogic and Including Code
Nested InclusionsNested Inclusions
Basic File InclusionBasic File InclusionUses the Uses the require require or or loadload command command to load the external file into the to load the external file into the current programcurrent program
#file: string_extensions.rb#file: string_extensions.rbclass Stringclass String def vowelsdef vowels self.scan(/[aeiou]/i)self.scan(/[aeiou]/i) endendendend
#file: vowel_test.rb#file: vowel_test.rbrequire 'string_extensions'require 'string_extensions'puts "This is a test".vowels.join('-')puts "This is a test".vowels.join('-')
Basic File Inclusion Basic File Inclusion (Cont.)(Cont.)Ruby programmers generally use Ruby programmers generally use requirerequire rather than rather than loadload. The effects . The effects of of loadload are only useful if the code in are only useful if the code in the external file has changed or if it the external file has changed or if it contains active code that will be contains active code that will be executed immediatelyexecuted immediately
#file: b.rb#file: b.rbload 'a'load 'a'puts "Hello from b.rb"puts "Hello from b.rb"load 'a'load 'a'puts "Hello again from b.rb"puts "Hello again from b.rb"
#result output#result outputHello from a.rbHello from a.rbHello from b.rbHello from b.rbHello from a.rbHello from a.rbHello again from b.rbHello again from b.rb
#file: a.rbputs "Hello from a.rb"
Inclusions from Other Inclusions from Other DirectoriesDirectoriesThe process of The process of requirerequire and and loadload
First looks the file in the current First looks the file in the current folderfolder
Second looks the file in the various Second looks the file in the various folders where Ruby stores its own folders where Ruby stores its own file and librariesfile and libraries
Lists of directories to search for Lists of directories to search for included files in a special variable included files in a special variable called $:.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...
Inclusions from Other Inclusions from Other Directories (Cont.)Directories (Cont.)Adds extra folders to search the fileAdds extra folders to search the file
$: is an array, so you can push extra $: is an array, so you can push extra items to ititems to it
$:.push '/your/directory/here'require 'yourfile'
Logic and Including Logic and Including CodeCodeAdds logic to the Adds logic to the require require oror load load commandcommand
$debug_mode = 0require $debug_mode == 0 ? "normal-classes" : "debug-classes"
%w{file1 file2 file3 file4 file5}.each { |l| require l }
Nested InclusionsNested InclusionsCode from files that are included into others Code from files that are included into others with with requirerequire and and loadload has the same freedom has the same freedom as if the code were pasted directly into the as if the code were pasted directly into the original file.original file.#file: a.rbrequire '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
LibrariesLibrariesCollection of routines that can be Collection of routines that can be called by separate programs, but called by separate programs, but that exist independently of those that exist independently of those programs.programs.
A list of all the standard libraries, A list of all the standard libraries, including documentation, is including documentation, is available at http://www.ruby-available at http://www.ruby-doc.org/stdlib/doc.org/stdlib/
How to use two standard librariesHow to use two standard libraries
net/http Librarynet/http LibraryHTTP stands for HyperText Transfer Protocol, HTTP stands for HyperText Transfer Protocol, and it’s the main protocol that makes the and it’s the main protocol that makes the World Wide Web work, as it provides the World Wide Web work, as it provides the mechanism by which Web pages, files, and mechanism by which Web pages, files, and other media can be sent between Web other media can be sent between Web servers and clientsservers and clientsrequire '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)endcontent = response.body
OpenStruct LibraryOpenStruct LibraryMore flexible than StructMore flexible than Struct
Creates data objects without Creates data objects without specifying the attributesspecifying the attributes
Create attributes on the flyCreate attributes on the fly
#structPerson = Struct.new(:name, :age)me = Person.new("Fred Bloggs", 25)me.age += 1
#openStructrequire 'ostruct'person = OpenStruct.newperson.name = "Fred Bloggs"person.age = 25
RubyGemsRubyGemsPackaging system for Ruby Packaging system for Ruby programs and libraries.programs and libraries.
Manages different versions of the Manages different versions of the same libraries on your PCsame libraries on your PC
Installing RubyGemsInstalling RubyGemsBefore you can use RubyGems, it’s Before you can use RubyGems, it’s necessary to install itnecessary to install it
On Windows, if you installed Ruby On Windows, if you installed Ruby using the “one-click installer”, you using the “one-click installer”, you already have RubyGem installedalready have RubyGem installed
On Mac OS X, Linux, and Other Unix On Mac OS X, Linux, and Other Unix
After you installed you can type After you installed you can type gemgem
Finding GemsFinding GemsLists of the gems that are installed Lists of the gems that are installed
gem list
Queries the remote gem serverQueries the remote gem server
gem list --remote
gem query --remote --name-matches class
Queries the remote gem server Queries the remote gem server and match the name “class”and match the name “class”
Installing a Simple GemInstalling a Simple GemInstalls “feedtools”Installs “feedtools”
gem install feedtools
#if it can't install probablysudo gem install feedtools
Using GemsUsing GemsRubyGems system isn’t an RubyGems system isn’t an integrated part of Rubyintegrated part of Ruby
It’s necessary to tell your It’s necessary to tell your programs that you want to use programs that you want to use and load gemsand load gems
require 'rubygems'require 'RedCloth'r = RedCloth.new("this is a *test* of _using RedCloth_")puts r.to_html
Installing a More Installing a More Complex GemComplex GemYou may want to install You may want to install “hpricot” in specific source for “hpricot” in specific source for latest versionlatest versiongem install hpricot --source code.whytheluckystiff.net
Upgrading and Upgrading and Uninstalling GemsUninstalling GemsUpdate all of your currently installed Update all of your currently installed gemsgemsgem update
Uninstall gems “feedtools” Uninstall gems “feedtools”
gem uninstall feedtools
$ gem uninstall rubyforge
Uninstall all gem versions Uninstall all gem versions “rubyforge”“rubyforge”
Thank-you for Thank-you for attendingattending
KhmerTech
Feel free to join at
http://groups.google.com/group/khmertech/