Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Sunday, March 27, 2011

Execute Shell Commands in Ruby

There are 3 ways in Ruby to execute Shell commands, each does it a little differently.

exec 'command'
This will execute the command and exit the Ruby script.

system 'command'
This will execute the command but you have no way to access the results. You will simply get a true or false indicating if the command was able to be executed. Call $? to get more information on the exit conditions

`command`
This will execute the command and give you access to any returned strings.

Create Class Dynamically in Ruby

Sample
def create_class_dynamically(mybinding)
clazz = 'class MyClass; end'
Kernel.eval(clazz, mybinding)
end

create_class_dynamically(binding)

Connect to SOAP Server in Ruby

Simple Example
require 'soap/wsdlDriver'

def self.remote_wsdl(url)
wsdl = SOAP::WSDLDriverFactory.new(url)
begin
return wsdl.create_rpc_driver
rescue
return wsdl.create_driver
end
end