Syntax error, unexpected '}', expecting keyword_end}

I'm new to Ruby, I don't quite understand what's going on here,

This code works

chrome = Watir::Browser.new:chrome
firefox = Watir::Browser.new:firefox
ie = Watir::Browser.new:ie

browser_conf = {
    "chrome" => chrome,
    "firefox" => firefox,
    "ie" => ie
}

      

But the following code creates a syntax error:

browser_conf = {
    "chrome" => Watir::Browser.new:chrome,
    "firefox" => Watir::Browser.new:firefox,
    "ie" => Watir::Browser.new:ie
}

      

syntax error, unexpected '}', expecting end_word}

Why is that? Is there a way that I can write implicitly without assigning them to a separate variable?

+3


source to share


4 answers


There is a little lax syntax here with one colon (:) at the end of the call new

. For example.

chrome = Watir::Browser.new:chrome

      

This is probably confusing the hash parser that can use colons as a key / value separator since ruby ​​1.9. For example.

hash = {foo: 'bar'}

      

You can always force a piece of code to be evaluated regardless of its environment, however, by enclosing it in parentheses. You will probably find that this code works:



browser_conf = {
  "chrome" => (Watir::Browser.new:chrome),
  "firefox" => (Watir::Browser.new:firefox),
  "ie" => (Watir::Browser.new:ie)
}

      

Update: I just checked the Watir docs and I believe your calling is new

unrealistic. The symbol for the browser is an argument new

. So either it should be after a space or in brackets. Without a space or parentheses, it can't always parse it as an argument.

Here's the correct code:

browser_conf = {
  "chrome" => Watir::Browser.new(:chrome),
  "firefox" => Watir::Browser.new(:firefox),
  "ie" => Watir::Browser.new(:ie)
}

      

+6


source


I think this is the problem here

chrome = Watir::Browser.new:chrome
firefox = Watir::Browser.new:firefox
ie = Watir::Browser.new:ie

      

Change this to (paranthesis for argument)



chrome = Watir::Browser.new(:chrome)
firefox = Watir::Browser.new(:firefox)
ie = Watir::Browser.new(:ie)

      

or before (space before argument)

chrome = Watir::Browser.new :chrome
firefox = Watir::Browser.new :firefox
ie = Watir::Browser.new :ie

      

+4


source


The way of passing arguments without parentheses can easily become ambiguous. Take this hash literal for example:

{ 1 => foo 2, 3 => 4 }

      

This can be interpreted as:

{ 1 => foo(2, 3 => 4) }

      

or how:

{
  1 => foo(2),
  3 => 4
}

      

The same for an array literal:

[foo 2, 3, 4]

      

This could be:

[foo(2, 3, 4)]
[foo(2, 3), 4]
[foo(2), 3, 4]

      

When parsing Ruby encounters this ambiguity, it usually raises the value SyntaxError

. Then you need to specify the parentheses explicitly.

In your case:

{ 'chrome' => Watir::Browser.new(:chrome) }

      

+3


source


You are missing a space before the browser name argument. Give paranthesis or space. Alternatively to do this dynamically .. any of these posts will help you on stackoverflow -

If you want to invoke another environment via the command line go to - link-one

For Ruby symbols to be transferred use link - link-two

Otherwise,

Comparing this to Watir vs Webdriver please visit - External link

You need to modify env.sh and make sure you pass in "browser type" or an equivalent variable that you can reference in the config file and instantiate accordingly. Hope this helps!

+1


source







All Articles