Learning Ruby 8 Exploiting Libraries Rubys Builtin Libraries

Learning Ruby - 8 Exploiting Libraries

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

Example Library - Base 64 require 'base 64' # Conforms with RFC 2045. input = "Let us turn our thoughts todayn" + "To Martin Luther Kingn" + "And recognize that there are ties between us" puts e_input = Base 64. encode 64( input ) puts Base 64. decode 64( e_input )

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" dt 2 = Date. Time. new( 2006, 9, 21 ) puts "It was a #{dt 2. strftime( '%A' )} when you “ + “started learning Ruby. "

Example Library - Digests require 'digest/md 5' require 'digest/sha 1' input = "Let us turn our thoughts todayn" + "To Martin Luther Kingn" + "And recognize that there are ties between us" puts "MD 5: " + Digest: : MD 5. hexdigest( input ) puts "SHA 1: " + Digest: : SHA 1. hexdigest( input )

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

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

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

Example Libraries - Windows require 'Win 32 API' # Do your worst. . . require 'win 32 ole' browser = WIN 32 OLE. new( “Firefox. Application" ) browser. visible browser. navigate( "http: //www. google. com" )

Example Library - Zlib require 'zlib' Zlib: : Gzip. Writer. 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: : Gzip. Reader. new( fh ) chunk = gzip. read. split( /n/ ) puts chunk end # of do.

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
- Slides: 11