Ruby Projects and Libraries
Upcoming SlideShare
Loading in...5
×
 

Ruby Projects and Libraries

on

  • 914 views

 

Statistics

Views

Total Views
914
Views on SlideShare
913
Embed Views
1

Actions

Likes
0
Downloads
4
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • 06/28/09 11:21 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Ruby Projects and Libraries Ruby Projects and Libraries Presentation Transcript

  • Ruby Projects and Libraries Vorleak Chy (vorleak.chy@gmail.com) Software Engineer Yoolk Inc http://groups.google.com/group/khmertech/
  • Agenda
    • Projects and Using Code from Other Files
    • Libraries
    • RubyGems
  • Introduction
    • 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 ...
  • Inclusions from Other Directories (Cont.)
    • Adds extra folders to search the file
    • $: is an array, so you can push extra items to it
    $:.push '/your/directory/here' require 'yourfile'
  • Logic and Including Code
    • Adds logic to the require or load command
    $debug_mode = 0 require $debug_mode == 0 ? "normal-classes" : "debug-classes" %w{file1 file2 file3 file4 file5}.each { |l| require l }
  • Nested Inclusions
    • 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”
  • Installing a Simple Gem
    • Installs “feedtools”
    gem install feedtools #if it can't install probably sudo gem install feedtools
  • Using Gems
    • RubyGems system isn’t an integrated part of Ruby
    • It’s necessary to tell your programs that you want to use and load gems
    require 'rubygems' require 'RedCloth' r = RedCloth.new("this is a *test* of _using RedCloth_") puts r.to_html
  • Installing a More Complex Gem
    • You may want to install “hpricot” in specific source for latest version
    gem install hpricot --source code.whytheluckystiff.net
  • Upgrading and Uninstalling Gems
    • Update all of your currently installed gems
    gem update
    • Uninstall gems “feedtools”
    gem uninstall feedtools $ gem uninstall rubyforge
    • Uninstall all gem versions “rubyforge”
  •  
  • Thank-you for attending Feel free to join at http ://groups.google.com/group/khmertech/ KhmerTech