Codeformer
Codeformer

Reputation: 2300

syntax error, unexpected '}', expecting keyword_end }

I am new to Ruby, I don't quite get what is happening 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 produces a syntax error:

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

syntax error, unexpected '}', expecting keyword_end }

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

Upvotes: 2

Views: 3365

Answers (4)

Stefan
Stefan

Reputation: 114138

Passing methods arguments without parentheses can easily become ambiguous. Take this hash literal for example:

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

That could be interpreted either as:

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

or as:

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

Same for this array literal:

[foo 2, 3, 4]

That could be:

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

When Ruby's parser encounters such ambiguity, it usually raises a SyntaxError. You then have to explicitly provide parentheses.

In your case:

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

Upvotes: 3

Ursus
Ursus

Reputation: 30056

I think here is the issue

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

Change that to (paranthesis for argument)

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

or to (space before argument)

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

Upvotes: 4

nitinr708
nitinr708

Reputation: 1467

You are missing an space before the argument of browser name. Give paranthesis or a space. Alternatively for doing it dynamically.. any of these posts will help you on stackoverflow -

If you want to call different environment through command line go to - link-one

For Ruby symbols to be passed take reference from - link-two

Otherwise,

Comparison between doing it with Watir vs Webdriver, please visit - External Link

You need to make changes to env.sh and ensure you pass 'browser-type' or equivalent variable which you can refer in config file and instantiate respective one. Hope it helps!

Upvotes: 1

AJFaraday
AJFaraday

Reputation: 2450

There's some slightly unsual syntax in play here, with a single colon (:) at the end of the new call. E.g.

chrome = Watir::Browser.new:chrome

This is probably confusing the hash parser, which can use colons as a key/value separater, since ruby 1.9. E.g.

hash = {foo: 'bar'}

You can always force a piece of code to be evaluated regardless of it's surroundings, however, by wrapping it in brackets. You'll probably find this code works:

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

Update: I've just checked the Watir docs, and I believe you're calling new incorrecly. The symbol for the browser is an argument to new. So should either be after a space, or in brackets. Without either white space or brackets, it's not always able to parse that 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)
}

Upvotes: 6

Related Questions