[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help finding this syntax error

Patrick Li

8/1/2008 8:13:00 PM

<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
--
Posted via http://www.ruby-....

13 Answers

Ståle Z.H

8/1/2008 8:19:00 PM

0

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


Patrick Li skrev:
> <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
> --
> Posted via http://www.ruby-....

Patrick Li

8/1/2008 8:22:00 PM

0

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 via http://www.ruby-....

Lee Jarvis

8/1/2008 8:27:00 PM

0

On Aug 1, 9:22 pm, Patrick Li <patrickli_2...@hotmail.com> 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-....

Remove the outside parenthesis

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

Regards,
Lee

Patrick Li

8/1/2008 8:31:00 PM

0

Ah that works well.

Is there any reason for the outside parenthesis to screw it up?
--
Posted via http://www.ruby-....

Adam Shelly

8/1/2008 8:33:00 PM

0

On 8/1/08, Patrick Li <patrickli_2001@hotmail.com> 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')
^

Patrick Li

8/1/2008 8:37:00 PM

0

hmm okay. maybe it'll be fixed in a later version.
Thanks a lot for your help guys.
--
Posted via http://www.ruby-....

Gregory Brown

8/1/2008 8:41:00 PM

0

On Fri, Aug 1, 2008 at 4:33 PM, Adam Shelly <adam.shelly@gmail.com> wrote:
> On 8/1/08, Patrick Li <patrickli_2001@hotmail.com> 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)

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/sa... | Non-tech stuff at:
http://metametta.bl...

Patrick Li

8/1/2008 8:54:00 PM

0

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?
--
Posted via http://www.ruby-....

Gregory Brown

8/1/2008 9:06:00 PM

0

On Fri, Aug 1, 2008 at 4:53 PM, Patrick Li <patrickli_2001@hotmail.com> wrote:
> 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>'

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

Gregory Brown

8/1/2008 9:09:00 PM

0

On Fri, Aug 1, 2008 at 5:06 PM, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On Fri, Aug 1, 2008 at 4:53 PM, Patrick Li <patrickli_2001@hotmail.com> wrote:
>> 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

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