Help finding this syntax error

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

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

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

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

Patrick Li skrev:

On Aug 1, 9:22 pm, Patrick Li [email protected] wrote:

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

Posted viahttp://www.ruby-forum.com/.

Remove the outside parenthesis

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

Regards,
Lee

On 8/1/08, Patrick Li [email protected] wrote:

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’)
^

Ah that works well.

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

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

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?

On Fri, Aug 1, 2008 at 4:33 PM, Adam S. [email protected]
wrote:

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.

if true and p 1
p :hi
end
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


Killer Ruby PDF Generation named after a magnificent sea creature:
http://github.com/sandal/prawn | Non-tech stuff at:
http://metametta.blogspot.com

On Fri, Aug 1, 2008 at 5:06 PM, Gregory B.
[email protected] wrote:

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. :slight_smile:

-greg

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.

On Fri, Aug 1, 2008 at 4:53 PM, Patrick Li [email protected]
wrote:

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 `’

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 :

h = { ‘aaa’ => 1 }
=> {“aaa”=>1}

if h.has_key? ‘aaa’ && h.length > 0 then “ok” else “nok” end
=> “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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs