user137369
user137369

Reputation: 5714

Variable in the middle of a command, in ruby

I have the following code

require 'watir-wedriver'

browsers = ['chrome', 'safari']

browsers.each do |browserName|
    browser = Watir::Browser.new :browserName
    # more code here
end

browserName doesn’t work here since the command is interpreting it literally (i.e., it’s reading browser = Watir::Browser.new :browserName instead of browser = Watir::Browser.new :chrome followed by browser = Watir::Browser.new :safari.

How can I make that variable expand there?

Upvotes: 0

Views: 52

Answers (1)

daniel gratzer
daniel gratzer

Reputation: 53911

Don't literally quote the variable

browsers.each do |browserName|
    browser = Watir::Browser.new browserName.to_sym # string to a symbol
    ...
 end

or just originally use symbols

 browsers = [:chrome, :safari]
 ...
     browser = Watir::Browser.new browserName

Upvotes: 1

Related Questions