160 likes | 297 Views
Integrating with Ruby?. Jeetendra Kukreja Brown Bag Seminar 03/31/2006. This presentation talks about…. Integration: Ruby as a glue Interesting article by Hal Fulton Running Multiple Processes A Little Integration Tool Why Ruby Sucks. Integration: Ruby as a glue.
E N D
Integrating with Ruby? Jeetendra Kukreja Brown Bag Seminar 03/31/2006
This presentation talks about… • Integration: Ruby as a glue • Interesting article by Hal Fulton • Running Multiple Processes • A Little Integration Tool • Why Ruby Sucks
Integration: Ruby as a glue • Subsystems need to talk to each other • Subsystems may not be written using the same language • Integration needs to be automated • Software Studio • Ruby helps
Interesting article by Hal Fulton • Software components and applications need to interoperate • Need to leverage the power of an existing library or application • At the same time harnessing the power of Ruby itself • Use Ruby binding for the library or app
Interesting article by Hal Fulton • Resources Available • C API • Simplified Wrapper and Interface Generator SWIG • RubyInline (embed C/C++ external module code in your Ruby) • GraphViz: used to analyze and draw mathematical graphs • ImageMagick: used to create and manipulate images
Interesting article by Hal Fulton • and more … • CVS and Subversion • Interaction with Google search, with Amazon, and with Pay Pal • GUI toolkits and frameworks: Qt, Tk, GTK+, Fox, and others • Sound, Multimedia, Document Formatting • PDF and Flash manipulation, XML processing • YAML marshaling, Web services, and more. • MySQL, PostgreSQL, Oracle, LDAP, and others • DBI library: DB-independent code • JRuby: Ruby interpreter in Java
Interesting article by Hal Fulton • and more … • RJNI: an interface to JNI for Ruby • JDWP (the Java Debug Wire Protocol) simultaneous controlling and monitoring of hundreds of Java agents over the wire • Parrot and .NET • SOAP, RSS • Win32API and Win32OLE library come with Ruby • COM, Edit the registry • Use the SAPI, and more • ActiveScriptRuby • Welcome to Ruby!
Running Multiple Processes • Spawning New Processes • Kernel::system e.g. system("tar xzf test.tgz") • backquotes e.g. result = `date` • IO.popen pig = IO.popen("pig", "w+") pig.puts "ice cream after they go to bed" pig.close_write puts pig.gets
Running Multiple Processes • (``--'') e.g. pipe = IO.popen("-","w+") if pipe pipe.puts "Get a job!" $stderr.puts "Child says '#{pipe.gets.chomp}'" else $stderr.puts "Dad says '#{gets.chomp}'" puts "OK" end • Unix calls • fork, exec, pipe, open
Running Multiple Processes • Independent Children • exec exec("sort testfile > output.txt") if fork == nil # The sort is now running in a child process # carry on processing in the main program # then wait for the sort to finish Process.wait trap("CLD") { pid = Process.wait puts "Child pid #{pid}: terminated" exit } exec("sort testfile > output.txt") if fork == nil # do other stuff...
Running Multiple Processes • Blocks and Subprocesses • Pass popen a command, such as date, and the block will be passed an IO object as a parameter. IO.popen ("date") { |f| puts "Date is #{f.gets}" } produces: Date is Sun Nov 25 23:43:36 CST 2001 • The IO object will be closed automatically when the code block exits • Code block associated with a fork will be run in a Ruby subprocess, and the parent will continue after the block fork {puts "In child, pid = #$$“;exit 99} pid = Process.wait puts "terminated, pid = #{pid},exit code = #{$? >> 8}” produces: In child, pid = 19333 terminated, pid = 19333, exit code = 99
.Class = Win32API • require 'Win32API' • Access to any arbitrary Windows 32 function. • Many of these functions take or return a Pointer datatype---a region of memory corresponding to a C string or structure type. • Represented using class String, which contains a sequence of 8-bit bytes. It is up to you to pack and unpack the bits in the String. • new Win32API.new( dllname, procname, importArray, export ) • Returns a new object representing a Windows 32 API function. • dllname - name of the DLL containing the function e.g.``user32'' '‘ • procname - name of the desired function. • importArray is an array of strings representing the types of arguments to the function. • export is a string representing (case-ins) the return type of the function. • Strings ``n'' and ``l'' represent numbers, • ``i'' represent integers, • ``p'' represents pointers to data stored in a string, • ``v'' represents a void type (used for export parameters only). • instance methods • call wapi.call( [ args ]* ) -> anObject • Call wapi.Call( [ args ]* )
.Class = Win32OLE • class methods • connect WIN32OLE.connect( aString ) -> wapi • Returns a new OLE automation client connected to an existing instance of the named automation server. • const_load WIN32OLE.const_load( wapi, [aClass=WIN32OLE] ) -> nil • Defines the constants from the specified automation server as class constants in aClass. • new WIN32OLE.new( aString ) -> wapi • Returns a new OLE automation client connected to a new instance of the automation server named by aString. • instance methods [ ] wapi[ aString ] -> anObject • Returns the named property from the OLE automation object. • [ ]= wapi[ aString ] = aValue -> nil • Sets the named property in the OLE automation object. • each wapi.each {| anObj | block } -> nil • Iterates over each item of this OLE server that supports the IEnumVARIANT interface. • invoke wapi.invoke ( aCmdString, [args]* ) -> anObject • Invokes the command given in aCmdString with the given args. args may be a Hash of named parameters and values. You don't need to call invoke explicitly; this class uses method_missing to forward calls through invoke, so you can simply use the OLE methods as methods of this class.
.Class = Win32OLE_EVENT • Used in conjunction with the WIN32OLE to add callbacks for Windows 32 events. • class methods • message_loop WIN32OLE_EVENT.message_loop -> nil • Executes the Windows event loop, translating and dispatching events. • new WIN32OLE_EVENT.new ( anOle, aName ) -> wapi • Returns a new WIN32OLE_EVENT (an event sink) for the given WIN32OLE object and named event source. If aName is nil, it will attempt to use the default source and will raise a RuntimeError if it cannot find one. • instance methods • on_event wapi.on_event ( [anEvent] ) {| args | block }-> nil • Defines a callback for the named anEvent. If anEvent is nil, then this callback is associated with all events. The block will be given any arguments appropriate for this event.
A Little Integration Tool • Legacy tool integrator • lets just look at the code • Why Ruby Sucks • Speech.ppt
References • Five Things You Didn't Know You Could Do with Ruby –Hal Fulton • http://www.devsource.com/article2/0,1895,1778695,00.asp • "Programming Ruby - The Pragmatic Programmer's Guide“ – David Thomas and Andrew Hunt • The Top 10 Reasons The Ruby Programming Language Sucks • http://www.rubydoc.org/docs/