1 / 11

Learning Ruby - 8

Exploiting Libraries. Learning Ruby - 8. Ruby's Built-in Libraries. There's a large collection of standard libraries provided with Ruby The PickAxe provides a comprehensive survey of what's available

rsilver
Télécharger la présentation

Learning Ruby - 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exploiting Libraries Learning Ruby - 8

  2. Ruby's Built-in Libraries • There's a large collection of standard libraries provided with Ruby • The PickAxe provides a comprehensive survey of what's available • The Internet hosts a number of Ruby Library Repositories, with The Ruby Application Archive (RAA) and RubyForge the most popular • The move is on to use RubyGems as the standard library management tool • Don't reinvent the wheel (life is far too short) - use Ruby libraries whenever you can

  3. Example Library - Base64 require 'base64' # Conforms with RFC 2045. input = "Let us turn our thoughts today\n" + "To Martin Luther King\n" + "And recognize that there are ties between us" puts e_input = Base64.encode64( input ) puts Base64.decode64( e_input )

  4. Example Library - Date require 'date' dt = Date.new( 2006, 9, 21 ) puts dt.to_s puts "The month is " + dt.month.to_s puts "This is the " + dt.yday.to_s + "th day of the year" dt2 = DateTime.new( 2006, 9, 21 ) puts "It was a #{dt2.strftime( '%A' )} when you “ + “started learning Ruby."

  5. Example Library - Digests require 'digest/md5' require 'digest/sha1' input = "Let us turn our thoughts today\n" + "To Martin Luther King\n" + "And recognize that there are ties between us" puts "MD5: " + Digest::MD5.hexdigest( input ) puts "SHA1: " + Digest::SHA1.hexdigest( input )

  6. Example Library - Find require 'find' Find.find( "/home/barryp/ruby/bin" ) do |fh| print "\t#{fh}\n" end # of do.

  7. Example Library - Readline require 'readline' input = Readline::readline( "Gimme some: ", true ) puts input input = Readline::readline( "Gimme some more: ", true ) puts input

  8. Example Library - Readline (again) require 'readline' include Readline input = readline( "Gimme some: ", true ) puts input input = readline( "Gimme some more: ", true ) puts input

  9. Example Libraries - Windows require 'Win32API' # Do your worst ... require 'win32ole' browser = WIN32OLE.new( “Firefox.Application" ) browser.visible browser.navigate( "http://www.google.com" )

  10. Example Library - Zlib require 'zlib' Zlib::GzipWriter.open( "ruby.gz" ) do |chunk| chunk.write( File.read( "ruby.data.txt" )) end # of do. require 'zlib' File.open( "ruby.gz" ) do |fh| gzip = Zlib::GzipReader.new( fh ) chunk = gzip.read.split( /\n/ ) puts chunk end # of do.

  11. More ... Ruby So Far • Get the know the standard libraries • Get to know the on-line repositories • Reuse whenever possible • Share your work with others - upload your libraries to the repositories

More Related