Help finding this syntax error

P

Patrick Li

<code>
if(FieldTypes.key? field && FieldTypes[field].respond_to?:select)
puts "selectable"
</code>

Hi, I'm having trouble writing the above line correctly. It works if I
explicitly put:

FieldTypes[field].respond_to?:)select)

But I don't understand why the parenthesis are required in this case.

Thanks a lot for helping
-Cuppo
 
S

Ståle Z.H

I think you need a space between respond_to? and :select:
if(FieldTypes.key? field && FieldTypes[field].respond_to? :select)


Patrick Li skrev:
 
P

Patrick Li

Nope. that's not quite it. i'm really quite stumped.

syntax error, unexpected tSYMBEG, expecting ')' (SyntaxError)
if(FieldTypes.key? field && FieldTypes[field].respond_to? :select)
^ from
-e:1
 
L

Lee Jarvis

Nope. that's not quite it. i'm really quite stumped.

syntax error, unexpected tSYMBEG, expecting ')' (SyntaxError)
    if(FieldTypes.key? field && FieldTypes[field].respond_to? :select)
                                                               ^  from
-e:1

Remove the outside parenthesis

if FieldTypes.key? field && FieldTypes[field].respond_to? :select

Regards,
Lee
 
P

Patrick Li

Ah that works well.

Is there any reason for the outside parenthesis to screw it up?
 
A

Adam Shelly

Nope. that's not quite it. i'm really quite stumped.

syntax error, unexpected tSYMBEG, expecting ')' (SyntaxError)
if(FieldTypes.key? field && FieldTypes[field].respond_to? :select)
^ from
-e:1

Apparently the parser just can't recognize function calls inside
conditions unless they have the parens:

irb(main):013:0> if (true && p 1)
irb(main):014:1> p :hi; end
SyntaxError: compile error
(irb):13: syntax error, unexpected tINTEGER, expecting kDO or '{' or '('
if (true && p 1)
^

irb(main):015:0> while (true && "s".index 's')
irb(main):016:1> p :loop
irb(main):017:1> end
SyntaxError: compile error
(irb):15: syntax error, unexpected tSTRING_BEG, expecting ')'
while (true && "s".index 's')
^
 
P

Patrick Li

hmm okay. maybe it'll be fixed in a later version.
Thanks a lot for your help guys.
 
G

Gregory Brown

Nope. that's not quite it. i'm really quite stumped.

syntax error, unexpected tSYMBEG, expecting ')' (SyntaxError)
if(FieldTypes.key? field && FieldTypes[field].respond_to? :select)
^ from
-e:1

Apparently the parser just can't recognize function calls inside
conditions unless they have the parens:

irb(main):013:0> if (true && p 1)
irb(main):014:1> p :hi; end
SyntaxError: compile error
(irb):13: syntax error, unexpected tINTEGER, expecting kDO or '{' or '('
if (true && p 1)

This isn't actually the case, it has to do with the fact that && binds tightly.
'and' and 'or' have less tight binding and work as you'd expect.
1
:hi

So... if Ruby sees if (true && p 1), it parses it as:
if ((true && p) 1)

but using if (true and p 1), Ruby sees:

if (true and p(1))

HTH,

-greg
 
P

Patrick Li

Hmm that makes more sense.
But now i'm confused why my code worked after removing the outside
parenthesis.

when i type this:
if FieldTypes.key? field && FieldTypes[field].respond_to? :select

shouldn't ruby be seeing this?
if (FieldTypes.key? field && FieldTypes[field].respond_to?) :select

which is still a syntax error?

Am i calculating what i think i'm calculating?
 
G

Gregory Brown

Hmm that makes more sense.
But now i'm confused why my code worked after removing the outside
parenthesis.

when i type this:
if FieldTypes.key? field && FieldTypes[field].respond_to? :select

shouldn't ruby be seeing this?
if (FieldTypes.key? field && FieldTypes[field].respond_to?) :select

which is still a syntax error?

Am i calculating what i think i'm calculating?

No idea. Adapting your code a bit but using a similar syntax:
hash = {} => {}
field = :foo => :foo
if hash.key? field && hash[field].respond_to? :select
p 'foo'
end
SyntaxError: (irb):53: syntax error, unexpected tSYMBEG, expecting
keyword_then or ';' or '\n'
if hash.key? field && hash[field].respond_to? :select
^
(irb):55: syntax error, unexpected keyword_end, expecting $end
from /Users/sandal/lib/ruby19/bin/irb:12:in `<main>'
 
G

Gregory Brown

Hmm that makes more sense.
But now i'm confused why my code worked after removing the outside
parenthesis.

when i type this:
if FieldTypes.key? field && FieldTypes[field].respond_to? :select

shouldn't ruby be seeing this?
if (FieldTypes.key? field && FieldTypes[field].respond_to?) :select

which is still a syntax error?

Am i calculating what i think i'm calculating?

No idea. Adapting your code a bit but using a similar syntax:

This all having been said, the point of leaving off parens is to make
your code clearer to read in places.
Having to understand esoteric parsing rules does not contribute to
that, so the way I'd suggest writing this is:

if FieldTypes[field].respond_to? :select

since the key? check isn't really doing anything for you. A key won't
be autovivified, and Nil does not respond to select,
a = {} => {}
a[:foo] => nil
a => {}
nil.respond_to?:)select)
=> false

Or if you prefer to leave it at is, just use normal parens:

if FieldTypes.key?(field) && FieldTypes[field].respond_to?:)select)

People will thank you for the extra 4 chars. :)

-greg
 
P

Patrick Li

lol okay...
thanks a lot for helping me with this.

I was pretty eager to jump on the parenthesis-less bandwagon, but i'm
gonna continue explicitly using them for a while, until these
ambiguities get resolved.
 
F

F. Senault

Le 1 août 2008 à 22:53, Patrick Li a écrit :
Hmm that makes more sense.
But now i'm confused why my code worked after removing the outside
parenthesis.

when i type this:
if FieldTypes.key? field && FieldTypes[field].respond_to? :select

shouldn't ruby be seeing this?
if (FieldTypes.key? field && FieldTypes[field].respond_to?) :select

Nope. It sees :

if FieldTypes.key?(field && FieldTypes[field].respond_to?:)select))

Example :
=> "nok"

But...
h[true]=1 => 1
if h.has_key? 'aaa' && h.length > 0 then "ok" else "nok" end
=> "ok"

I always parenthesise the arguments unless the test is very simple (i.e.
"if hash.has_key? :toto" is ok, but nothing more).

Fred
 
P

Patrick Li

Ah I understand now. Thanks.
Yeah I think i'll stick to my lovely parenthesis for now.
I'm lucky that my code had a syntax error actually... i can't even
imagine having track down a bug like this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top