简体   繁体   中英

How to run multiple ruby scripts?

Is there a way to call a ruby script a within another ruby script b ? I have a ruby script which performs the website login ( login.rb ) and another script order_create.rb . I want to call login.rb first and then execute order_create.rb next. Please suggest. Order_Created.rb:-

@@order_data = YAML.load(File.open'C:\Users\order_details.yaml')                                                        def fill_order_form(order_data)
   fill_in 'Firstname', :with => order_data['firstname']
   fill_in 'Lastname', :with => order_data['lastname']
   fill_in 'ZIP', :with => order_data['zip']
   click_button 'Continue' 

   end

order_detail.yaml :-

firstname: "Order"
lastname: "Test"
zip: "90341"

login.rb:-

require './order_create.rb'
def login
    #login code here
     fill_order_form(@@order_data)
end

Error on running login.rb :-  undefined method `fill_order_form' for #<#<Class:0x3e344e0>:0x4248ba0>

A similar (although different) question already been answered at : Running another ruby script from a ruby script

You can include the script that you want to call in your script with:

require './b.rb' #if b.rb is in the same directory

and call it with:

b(args)

for your example, you can do the following:

login.rb

require './order_create.rb'
def login
    #login code here
    order_create()
end

Assuming that your create_order.rb contains def create_order()

Even though you can execute any shell command using backticks or %x

`ruby yourscript.rb`
%x(ruby yourscript.rb)

In this case it isn't good idea, since you have conventional ways of solving this, create third script, say login_and_order.rb and put following code inside:

require_relative 'login.rb'
require_relative 'order_create.rb'

# run your methods from both scripts in sequence you need
# or if they are just set of commands, nothing else needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM