0

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>
1
  • This depends on what you mean by "call". require will execute the script in the current Ruby process, system will open a new process which you can run the script with (and provide process-level arguments to), Open3 will let you open a new process to execute the script which you can then pipe input and output to/from, etc. Commented Feb 6, 2015 at 19:08

2 Answers 2

0

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()

Sign up to request clarification or add additional context in comments.

4 Comments

Got this error :---- `require': cannot load such file -- Login.rb (LoadError)
@Anusha If create_order.rb is in the same folder as login.rb then you could use require _relative 'order_create'. Same with wherever you are requiring login.rb
Thanks all. The above error was removed by using require as follow :- require './Order_Create.rb'
@Anusha I will edit this answer to reflect that. Thank you. Please mark it as best answer for anybody else coming here with this issue
0

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

7 Comments

Thanks! . I am able to call a script order_create.rb within another ruby script (login.rb) now . But unable to call the method defined in order_create.rb. Error Received :- undefined local variable or method `order_data'. Order_Created.rb is like this :- @@order_data = YAML.load(File.open'C:\Users\order_details.yaml') def fill_order_form(order_data) (Have defined all fields in yaml file) Can u please suggest how to handle this.
@Anusha so you call it like order_data? But @@order_data isn't a method, it's class variable(even though I am unable to understand why do you use it outside of the class)
I am calling it as fill_order_form(order_data) in login.rb . fill_order_form(order_data) is defined in create_order.rb.
Order_Data has file path of yaml file which has all values to fill the form and create order. My apologizes for confusion . i am new to Ruby. and trying hard to get thru . Thanks for your patience.
@Anusha can you just edit your question to show relevant parts from both scripts? From now, I see that you define @@order_data =, so you should call fill_order_form(@@order_data) accordingly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.