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.

Ruby Projects and Libraries


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Ruby Projects and Libraries

  1. 1. Ruby Projects and Libraries Vorleak Chy ( Software Engineer Yoolk Inc
  2. 2. Agenda <ul><li>Projects and Using Code from Other Files </li></ul><ul><li>Libraries </li></ul><ul><li>RubyGems </li></ul>
  3. 3. Introduction <ul><li>Using code already written and prepared by other developers within your own applications </li></ul>
  4. 4. Projects and Using Code from Other Files <ul><li>Basic File Inclusion </li></ul><ul><li>Inclusions from Other Directories </li></ul><ul><li>Logic and Including Code </li></ul><ul><li>Nested Inclusions </li></ul>
  5. 5. Basic File Inclusion <ul><li>Uses the require or load command to load the external file into the current program </li></ul>#file: string_extensions.rb class String def vowels self.scan(/[aeiou]/i) end end #file: vowel_test.rb require 'string_extensions' puts &quot;This is a test&quot;.vowels.join('-')
  6. 6. Basic File Inclusion (Cont.) <ul><li>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 </li></ul>#file: b.rb load 'a' puts &quot;Hello from b.rb&quot; load 'a' puts &quot;Hello again from b.rb&quot; #result output Hello from a.rb Hello from b.rb Hello from a.rb Hello again from b.rb #file: a.rb puts &quot;Hello from a.rb&quot;
  7. 7. Inclusions from Other Directories <ul><li>The process of require and load </li></ul><ul><li>First looks the file in the current folder </li></ul><ul><li>Second looks the file in the various folders where Ruby stores its own file and libraries </li></ul><ul><li>Lists of directories to search for included files in a special variable called $:. </li></ul>$:.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 ...
  8. 8. Inclusions from Other Directories (Cont.) <ul><li>Adds extra folders to search the file </li></ul><ul><li>$: is an array, so you can push extra items to it </li></ul>$:.push '/your/directory/here' require 'yourfile'
  9. 9. Logic and Including Code <ul><li>Adds logic to the require or load command </li></ul>$debug_mode = 0 require $debug_mode == 0 ? &quot;normal-classes&quot; : &quot;debug-classes&quot; %w{file1 file2 file3 file4 file5}.each { |l| require l }
  10. 10. Nested Inclusions <ul><li>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. </li></ul>#file: a.rb require 'b' #And b.rb contains the following: require 'c' #And c.rb contains the following: def example puts &quot;Hello!&quot; end #And d.rb contains the following: require 'a' example
  11. 11. Libraries <ul><li>Collection of routines that can be called by separate programs, but that exist independently of those programs. </li></ul><ul><li>A list of all the standard libraries, including documentation, is available at </li></ul><ul><li>How to use two standard libraries </li></ul>
  12. 12. net/http Library <ul><li>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 </li></ul>require 'net/http' Net::HTTP.get_print('', '/') require 'net/http' url = URI.parse('') response = Net::HTTP.start(, url.port) do |http| http.get(url.path) end content = response.body
  13. 13. OpenStruct Library <ul><li>More flexible than Struct </li></ul><ul><li>Creates data objects without specifying the attributes </li></ul><ul><li>Create attributes on the fly </li></ul>#struct Person =, :age) me =;Fred Bloggs&quot;, 25) me.age += 1 #openStruct require 'ostruct' person = = &quot;Fred Bloggs&quot; person.age = 25
  14. 14. RubyGems <ul><li>Packaging system for Ruby programs and libraries. </li></ul><ul><li>Manages different versions of the same libraries on your PC </li></ul>
  15. 15. Installing RubyGems <ul><li>Before you can use RubyGems, it’s necessary to install it </li></ul><ul><li>On Windows, if you installed Ruby using the “one-click installer”, you already have RubyGem installed </li></ul><ul><li>On Mac OS X, Linux, and Other Unix </li></ul><ul><li>After you installed you can type gem </li></ul>
  16. 16. Finding Gems <ul><li>Lists of the gems that are installed </li></ul>gem list <ul><li>Queries the remote gem server </li></ul>gem list --remote gem query --remote --name-matches class <ul><li>Queries the remote gem server and match the name “class” </li></ul>
  17. 17. Installing a Simple Gem <ul><li>Installs “feedtools” </li></ul>gem install feedtools #if it can't install probably sudo gem install feedtools
  18. 18. Using Gems <ul><li>RubyGems system isn’t an integrated part of Ruby </li></ul><ul><li>It’s necessary to tell your programs that you want to use and load gems </li></ul>require 'rubygems' require 'RedCloth' r =;this is a *test* of _using RedCloth_&quot;) puts r.to_html
  19. 19. Installing a More Complex Gem <ul><li>You may want to install “hpricot” in specific source for latest version </li></ul>gem install hpricot --source
  20. 20. Upgrading and Uninstalling Gems <ul><li>Update all of your currently installed gems </li></ul>gem update <ul><li>Uninstall gems “feedtools” </li></ul>gem uninstall feedtools $ gem uninstall rubyforge <ul><li>Uninstall all gem versions “rubyforge” </li></ul>
  21. 22. Thank-you for attending Feel free to join at http :// KhmerTech